If you need an item to be repeat at a set interval, you use var obj = setInterval(callback,duration) type of arrangement.
In your example this would likely look like
var s1obj = setInterval(s1,4000);
var s2obj = setInterval(s2,8000);
var s3obj = setInterval(s3,12000);
the use of obj is in case you wish to cease an interval timer by calling clearInterval( obj ); which will clear that timer.
You do not nest or "loop" them with a for loop. SetTimeout is not the way to set a timer to repeat because of the time lag that introduced and drag on site performance as your setting many timeouts and an interval only needs setting once.
Last edited by JunkMale; 10-19-2011 at 04:35 PM.
Reason: Stuff to add...
well it all depends on how many times you want the our functions to run, from your code and attempt to "loop" I took it to mean you want them to run more than once.
So you can set up a series of setInterval timers and give them an object that you can use in the s1,s2 & s3 functions to control the timer.
and all those functions will be called at those number of milliseconds. You would onlu need to wrap them in a function and have the document.body.onload = startItUp(); function to call that function to trigger the functions at regular intervals.
Last edited by JunkMale; 10-19-2011 at 04:52 PM.
Reason: Missin stuff...
If you use a JSON type arrangement then you could...
timer = {
startItUp:function(){
.... do stuff like set the other interval timers...
},
auto:setTimeout("timer.startItUp()",8000)
}
that BTW will set a timeout that calls the startItUp function after 8 seconds. It only runs onces, so any timers you set within that function will run at the intervals you set.
will cause those timers to be set 8 seconds after the page script loads, the page could still be loading which is why I suggested the document.body.onload method to ensure that the function is called when the document loads.
When set, the functions s1 to 3 will be called at those specified intervals.
jQuery what? why not learn to do it the easy way through plain old scripting...
which might make a difference. I would advice that you load from a fresh the page as you may have interval timers running... once set the interval timers will run until the page is closed or navigated away from.
I just tried a script with the change to the function call within quotes with () and it worked fine on my browser.
The way it is built currently:
if s1obj is fired every 5 seconds and s2obj is fired every 11 seconds then the result would be s1obj firing twice before s2obj fires for the first time.
Which differs from what I need:
s1obj fires after 5 seconds, 6 seconds later s2obj is fired, and another 6 seconds later s2obj is fired, Repeat.
s2 fires within 3 seconds, and s3 within 1 second. What is wrong?
s1obj = setInterval(s1,5000); has been called twice, 5 seconds + 5 seconds = 10 seconds - 8 seconds pause + 1 second lag = about 3 seconds apparent execution time.
Just to explain. It appears that to call setInterval(s3, triggers immediately and then at the set interval whereas setInterval("s3()", does not and only is called at the specified interval.
Bookmarks