views:

70

answers:

2

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
+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
Also worth noting, `clearTimeout()` will not through an exception if an invalid ID is passed (if the timeout already occured).
Robert
@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
Nick Craver thanks for answer
PokemonCraft