I noticed a search query pointing to my site looking for how to stop a javascript
So to combat this, you want to use the method
This will appear not to do anything, but what it actually does is set the timer then clear it out. Now when you store the value, you can trigger an event to call the
setTimeout
, and this is something I always forget. Here's the thing, when working with a timer, it emulates some form of multithreading/concurrency. Problems that can arise from this if you do things like timed animations on a triggered event are that if you don't stop a timer, you get multiple timers. This means at each trigger, you spawn a new timer to run with this emulated concurrency creating weird effects.So to combat this, you want to use the method
clearTimeout
. This will take the argument of the integer returned when you call setTimeout
. So when using this, you will also need to make sure you track that value.var x = setTimeout(function() { alert(""); }, 1000); clearTimeout(x);
This will appear not to do anything, but what it actually does is set the timer then clear it out. Now when you store the value, you can trigger an event to call the
clearTimeout
or you can initiate another setTimeout
to have the clearTimeout
run at some arbitrary time without a triggered event, like mathematically calculate a time to end some arbitrary animation without interrupting the main javascript code being executed or some such things. This method can also be called inside the timed function so long as it can clear the write variable, so make sure the scope is correct.
No comments:
Post a Comment