Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable all setTimeout events?

I am using ajax and asp.net. iI have a javascript function which creates many other javascript functions with setTimeout. After asynchronous postback happenes, I want to disable all of these setTimeouted events. How can I do that?

like image 854
MonsterMMORPG Avatar asked Oct 02 '10 18:10

MonsterMMORPG


People also ask

How do I turn off setTimeout?

To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.

Can we clear setTimeout?

No, setTimeout stops running after 1 call. In order to stop setInterval however, you must use clearInterval .

What is alternative to setTimeout?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.

Is there a way to clear the timeout of a setTimeout?

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.

How do I set the timerid of a setTimeout function?

let timerId = setTimeout( func | code, [ delay], [ arg1], [ arg2], ...) Function or a string of code to execute. Usually, that’s a function. For historical reasons, a string of code can be passed, but that’s not recommended. The delay before run, in milliseconds (1000 ms = 1 second), by default 0.

What happens when you cancel a call to setTimeout?

A call to setTimeout returns a “timer identifier” timerId that we can use to cancel the execution. In the code below, we schedule the function and then cancel it (changed our mind). As a result, nothing happens: As we can see from alert output, in a browser the timer identifier is a number. In other environments, this can be something else.

What is the difference between scheduled and setTimeout?

We may decide to execute a function not right now, but at a certain time later. That’s called “scheduling a call”. setTimeout allows us to run a function once after the interval of time.


1 Answers

If you can't get access to the code where the timer is set Nick's answer may not work, so all that I can think of is this hack.

It is a hack, use with caution!

// Set a fake timeout to get the highest timeout id var highestTimeoutId = setTimeout(";"); for (var i = 0 ; i < highestTimeoutId ; i++) {     clearTimeout(i);  } 

Basically it grabs the highest timer id and clears everything less than that. But it's also possible to clear other timers that you do not want to clear!

like image 167
SeanDowney Avatar answered Oct 21 '22 02:10

SeanDowney