So I know that you can assign setInterval to a variable, and later pass that variable to clearInterval to disable the timer.
What I'm wondering:
Say I have a global variable that is null on page initiation. When a specific function gets called then the global variable is assigned setInterval(fname, seconds). When I don't want this interval running anymore, I set the global variable to null.
For what I'm specifically testing (a sequence of images being rendered one after the other) it seems to work, but I wonder if I'm making some sneaky error.
Here's an overview of my code:
MyStuff.controls = {};
MyStuff.controls.animation_timer = null;
MyStuff.controls.start = function () {
if (!MyStuff.controls.animation_timer) {
MyStuff.controls.animation_timer = setInterval(MyStuff.controls.animate, 500);
}
}
MyStuff.controls.stop = function () {
MyStuff.controls.animation_timer = null;
}
No, that does not work. You can simply try out your code. It will continue executing your callback, even if you've thrown away the identifier that could have been used to stop the interval.
Just call
clearInterval: