PDA

View Full Version : hey guys, problem with looping :P


cat_evilness
08-09-2002, 02:59 AM
Hey everyone,

First of all I'd just like to say Hi because I'm new to this forum. And secondly I was wondering if anyone could help me with a problem I have:

Ok, I've just started using JavaScript because I need it for my project at school. I'm making a virtual pet and I want to have variables that control it's mood: hunger, health etc. Anyway I was gonna have these variables controlled by a loop that counts down.

I'm use to writing loops in Basic where you would write:
FOR loop = 1 TO 10 etc.
But I have no idea how to write a loop that counts down from 10 to 1 in JavaScript.

If anyone knows could you please help me.

Thankyou,
Cat :confused:

Note: As this is essentially a continuation of the thread you started on the 8th, I've merged the 2 threads together to avoid confusion. For future reference, please keep threads regarding the same question together by posting further problems as replies to your original thread ;).

boywonder
08-09-2002, 03:36 AM
for(i=10;i>0;i--) {
// code
}

OR

i=10
while (i >0) {
// code
i-- //must have this inside the loop
}

good luck with it

cat_evilness
08-09-2002, 03:51 AM
thankyou, i will give it a try!!

cat_evilness
08-09-2002, 04:24 AM
Hey again, how would you get it so that it prints out the number as it is counting down. for example 10 would become 9 etc....

Catman
08-09-2002, 04:31 AM
Drop in a document.write(i + " ") to get 10 9 8 7 6 5 4 3 2 1 all on one line..

cat_evilness
08-11-2002, 08:56 PM
Hey again everyone,

thankyou for your original help but it didn't do quite what i needed it to!

What I want is a countdown that counts down 10 seconds, saying 10. 9. 8, i don't want the numbers to appear on the same line but i want it to say 10 then a second later the 10 to be replaced with a 9, i hope you understand what i mean.

Thankyou,
Cat x :thumbsup:

Algorithm
08-11-2002, 09:24 PM
Assuming you're working in IE5+, NS6+, or Mozilla:

document.getElementById("countdown").innerHTML = i;

In order for this code to work, you must have a DIV element on your page named "countdown", like so:

<div id="countdown"></div>

I've greatly simplified the process for the sake of this example, so if you have any problems/questions, feel free to ask.

cat_evilness
08-11-2002, 09:40 PM
hey, i tried to mess around what you said but i ended up totally confused and it kept having the done with error thing in the status bar. here is the code for the page:
<script language="javascript">
<!--
function open () {return true;}
//-->
</script>
<html>
<head>
<title>Virtual Pet - Version 1.0</title>
</head>
<body bgcolor="#FF9900" text="#ffffff" link="#00ff00" alink="#ffa500" vlink="#ff0000">
<basefont size="3">
<br>


</form>
</body>
</html>

could you please type the code into the page above where it should be.

thankyou so much,
cat x