PDA

View Full Version : setInterval for countdown


Kevin
03-16-2003, 08:04 PM
Can you take a look at the script and tell me why the timer dosent' display in the status bar correctly. Should be a count down.

<html><head><title></title>
<script>
var count = 15;



function countDown (count) {
count--;
if(!count)
{
clearInterval (myTimer);
self.close();
}
return count;
}
var myTimer = setInterval ( "countDown(count)", 1000 );
window.status ="this window closes in " + count + " seconds"
</script>
</head><body>
<FORM name="myForm">
<table>
<tr>
<th>Choose the background color of the main window:</th>
<td><select name="BkGrnd" onchange="opener.changeBgColor(this)";>
<option value="red">Red</option>
<option value="blue">blue</option>
<option value="green">green</option>
<option value="yellow">yellow</option>
<option value="orange">orange</option>
<option value="black">black</option>
<option value="white" selected>white</option>
</select>
</td>
</tr>
<tr>
<th>Choose the text color of the main window:</th>
<td><select name="ForeGrnd" onchange="opener.changeFgColor(this)";>
<option value="red">Red</option>
<option value="blue">blue</option>
<option value="green">green</option>
<option value="yellow">yellow</option>
<option value="orange">orange</option>
<option value="black" selected>black</option>
<option value="white">white</option>
</select>
</td></tr></table>
</FORM></body></html>


insight always appreciated
Thank You
Kevin

cheesebagpipe
03-17-2003, 01:11 AM
Your status write is outside of the function, so it's only executed once. Try this:

<script type="text/javascript">

var count = 16;

function countDown() {
sCount = (--count<10) ? '0' + count : '' + count; //decrement counter, format seconds string
status = "this window closes in " + sCount + " seconds...";
if (count) setTimeout('countDown()',1000);
else self.close();
}

onload = countDown;

</script>

Kevin
03-17-2003, 01:31 AM
Can you tell me why my code failed?

Looking at your code I can see the improvements but still haven't been able to ascertain why mine didn't make it.

Thank You
Kevin

cheesebagpipe
03-17-2003, 01:51 AM
Already stated: the countDown function is looping in an interval, but the window.status=... statement is outside it. Why would it be executed more than once?

More to the point - there are some basic errors in your code...

1) function countDown (count) { ...this doesn't pass the value of the global 'count' to the function, it creates a local variable 'count' that masks the global one. You pass parameters in the function call...in this case, the value is in a global, so you don't need to pass it at all

2) return count; ....return...to what? You didn't call the function as part of an assignment, e.g., x = countDown(count)...might need to look up how returning values works

3) var myTimer = setInterval ( "countDown(count)", 1000 ); ..again, passing a global...this passes the value of the global 'count', which is assigned to the local 'count', which is decremented inside the function, then destroyed when it returns, leaving the global unchanged.

Pretty basic stuff. Might want to refresh your knowledge of core JS concepts before proceeding. ;)

Kevin
03-17-2003, 01:57 AM
I appreciate the help, I shouldn't have run into this much trouble.
My real trouble lay in how to call setInterval correctly.
After that I was just shooting in the dark in an attempt to make the function work. In doing so I forgot everything I ever learned about functions and passing parameters.

insight always appreciated

Thank You
Kevin