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?
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.
No, setTimeout stops running after 1 call. In order to stop setInterval however, you must use clearInterval .
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.
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.
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With