Thread: countdown
View Single Post
Old 04-03-2009, 06:32 AM   #2 (permalink)
Krik
The Contributor
 
Join Date: Feb 2009
Posts: 65
Thanks: 0
Krik is on a distinguished road
Default

HTML Code:
setTimeout("myfunc();", 1000);
that code with after 1 second run the "mufunc" function. If I have that inside of the "myfunc" function it will every second run the "myfunc" function in an indefinite loop.

If you wish to have it end after a variable number of loops
HTML Code:
var stopcount = 93;
var loopcount = 0;
function myfunc() {
loopcount++;
    if (loopcount == stopcount) {
    clearTimeout(counter);
    }
    else {
    counter = setTimeout("myfunc();", 1000);
    }
}
As you can see it looks a lot like PHP the only difference is in the internal functions. Basically any time I need to do something with javascript I go to google and type in javascript and the name of the equivalent PHP function and I usually find the javascript function or if it don't exist the work around that people have made to do the same thing.

EDIT
I should note that officially 1000 is a second. But in testing I have seen as little as 990 make a full second. So you may need to adjust. Of course I suspect this is based on the individual computer. If your going to count more than 10 minutes worth maybe reload the page and use the servers time to correct discrepancies.
Krik is offline  
Reply With Quote