Stopping Nested Timeouts in Javascript
I want to execute a piece of arbitrary code and be able to stop it
whenever I want. I figured I could do this with setTimeout and then use
clearTimeout to stop it. However if the code in the timeout creates it's
own timeouts, then those keep executing even after I clear the original.
Example:
var timeoutID = setTimeout(
function(){
console.log("first event can be stopped with
clearTimout(timeoutID)");
setTimeout(function(){console.log("but not this one")}, 5000)
}, 5000)
Now one way would be to control the code being executed and make it store
the value of any additional timeouts into a global variable and clear them
all at once. But is there a better way to do this? And is there a way to
do this on arbitrary code?
No comments:
Post a Comment