PDA

View Full Version : javascript: looping functions?


forlamp
04-14-2004, 12:19 PM
Ok.. is it possible to create a javascript function in a document, that constantly loops and wont interrupt the the flow of the document? I'd like to use a timer so it doesnt hog system resources, but lets say every 30 seconds i need this function to check an array 'like a stack', and remove lines off the stack... all going on while the user is browsing the page..

-fl

(im making u work today agent :) )

agent002
04-14-2004, 12:24 PM
yes you do but it's just fun :)

ok, so you have a JavaScript function doing the stuff you want I hope. You can make it repeat every 30 seconds by using setInterval():
function doStuff(){
// do stuff here...
}

var repeatDoStuff = setInterval('doStuff()', 30000);
The first argument passed to setInterval() is the function to execute, the second is the interval in milliseconds; 30s * 1000 = 30000ms. Storing the return value to a variable, like repeatDoStuff is optional, you do that to be able to eventually stop the repeating using clearInterval(repeatDoStuff).

forlamp
04-14-2004, 12:27 PM
heh, cool that saves me alot of worry..

and yeah i will need to keep the return value (or interval id?) so i can stop it and start it on and off all the time..

sweet :)

thanks.

-fl