Click to See Complete Forum and Search --> : Problem with setTimout


krisdc
05-24-2004, 03:32 AM
The purpose of my code is to make a countdown form 10 to 0 in window.status by clicking a button.

I've want it to count 1 down every second. But it goes straight to 0 :confused:

I really don't know what i'm doint wrong. I'm just a simple student who needs to know this for his exams. Can anyone help me? I've tried almost everything.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
<!--
var teller=10;
function aftellen()
{
while (teller>0)
{
setTimeout("window.status=teller",1000);
teller=teller-1;
}
}
//-->
</script>
</head>

<body>
<input type="button" value="knop1" onClick="aftellen()";>
</body>
</html>

krisdc
05-24-2004, 03:51 AM
I've tried it with a for to but is has the same result :(

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
<!--
var teller=10;
function aftellen()
{
for (teller;teller>0;teller=teller-1);
{
setTimeout("window.status=teller",1000);
}
}
//-->
</script>
</head>

<body>
<input type="button" value="knop1" onClick="aftellen()";>
</body>
</html>

simpson97
05-24-2004, 05:27 AM
Try this - it works:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
<!--
var teller = 10;
function countDown()
{
window.status = teller--;
if(teller < 0)
{
teller = 10;
clearInterval(mytimer);
}
}
//-->
</script>
</head>

<body>
<input type="button" value="knop1" onClick="mytimer=setInterval('countDown()',1000)";>
</body>
</html>

Bob

Khalid Ali
05-24-2004, 08:46 AM
or just a bit of adjustment to your code wil make it work


var teller=10;
function aftellen(){
if(teller>=0){
window.status=teller;
setTimeout("aftellen()",1000);
teller--;
}
}