a simple question for which I'm a bit cofused...
What is the meaning of the id returned by setInterval(), is it refering to the number of intervals instantiated in a variable, and if so is there a way to clear them all at once?
Many thanks for your answers,
The setInterval() returns the instance ID of that interval running in memory, this ID can be used to clear that interval with clearInterval(<id>) function.
For ex. the code below sets up two intervals and then clears the 1st interval.
var id1 = window.setInterval('some_function', 100);
var id2 = window.setInterval('some_other_function', 500);
I see thanks for the explanation...
I think my only question now is if the window. code has to be used on every scope or could I, inside a prototype, say something like...
Is the code above potentially not good enough so I end up firing lots of events in other functions?
I think you have a good start, just a couple of slight improvements.
if (this.interval == undefined)
if (this.interval != undefined)
this.interval = undefined;
This way you can't create an orphaned interval if you were to call imageSlider.start(); then call imageSlider.start() again before calling imageSlider.stop(). Likewise, you can't try to clear an undefined interval by calling imageSlider.stop() before you ever called imageSlider.start().
Lastly, in the stop(), I sent the this.interval back to undefined, because I believe it keeps the instance number even after you call clearInterval, so this now permits you to do imageSlider.start(); imageSlider.stop(); imageSlider.start();
Perfectly clear now,
thanks for taking the time...
This topic is now closed. New replies are no longer allowed.