hello. i am using ajax and asp.net . i have a javascript function which creates many setTimeouted other javascript functions . after asynchronous postback happened i want to disable all of this setTimeouted events. thank you.
+2
A:
Not sure if you can do this globally, but the most common method is to use clearTimeout. You pass the return value of setTimeout() to clearTimeout(), you could use a global var to store all timeout vars.
Robin
2010-10-02 19:01:33
+5
A:
When you call setTimeout()
store the timer ID so you can clear it. If you're creating many then an array is a good option, for example:
var timeouts = [];
//then, store when you create them
timeouts.push(setTimeout() { ... }, 1000));
Then when you want to clear them:
for(var i=0; i<timeouts.length; i++) clearTimeout(timeouts[i]);
timeouts = []; //quick reset of the timer array you just cleared
As @Robert notes below: to be clear, clearTimeout()
won't throw an error if the timeout has already occurred, so there are no race/timing issues here.
Nick Craver
2010-10-02 19:07:21
Also worth noting, `clearTimeout()` will not through an exception if an invalid ID is passed (if the timeout already occured).
Robert
2010-10-02 19:08:42
@Robert - Good point, I tend to take this for granted but I'll add it in, in case there's any concern over it :)
Nick Craver
2010-10-02 19:09:53
Nick Craver thanks for answer
PokemonCraft
2010-10-02 19:24:53