59

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?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

5 Answers5

122

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!

SeanDowney
  • 17,368
  • 20
  • 81
  • 90
  • Thanks to Vaidotas, I updated my set timeout function to be more browser friendly – SeanDowney Nov 08 '12 at 22:05
  • 16
    +1 this is a very handy snippet to paste into the Javascript console to turn off all timeouts! – Paul Dixon Mar 18 '13 at 13:16
  • 4
    This hack does cause hard-to-catch bugs because if a 3rd party library uses setTimout and setInterval for initialization, that libary would stop working for no reason as I just found out. – user2191332 Feb 28 '14 at 03:24
  • also this hack will be slower every time because of increasing timer id – nick.skriabin Sep 14 '15 at 13:21
  • This is an amazing idea. After all, the target is to clear ALL timeouts. +1 – Crystallize Jun 05 '18 at 07:18
  • I don't care what breaks. Death to all modal popups! – HonoredMule Jan 10 '19 at 23:21
  • Citing from https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout -- It's worth noting that the pool of IDs used by setTimeout() and setInterval() are shared, which means you can technically use clearTimeout() and clearInterval() interchangeably. --. This implies that by using this "hack", you will also clear intervals! – nmargaritis Oct 09 '19 at 14:59
  • greatest code i ever seen recent days. – Tayfun Yaşar Apr 28 '22 at 18:32
114

When you call setTimeout(), store the timer ID so you can clear it. If you're creating many timeouts, then an array is a good option for storing the IDs. 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]);
}
//quick reset of the timer array you just cleared
timeouts = [];

As @Robert noted below, clearTimeout() won't throw an error if the timeout has already occurred, so there are no race/timing issues here.

Community
  • 1
  • 1
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 6
    Also worth noting, `clearTimeout()` will not through an exception if an invalid ID is passed (if the timeout already occured). – Robert Oct 02 '10 at 19:08
  • @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 Oct 02 '10 at 19:09
  • I would like to think that with the javascript single-threaded model that: In all cases, once clearTimeout has completed there is a guarantee that the timeout event will not happen again. It is not in the docs, does this go without saying? – jcalfee314 Feb 02 '15 at 13:51
4

Firstly, I was using this code:

var x = setTimeout('');
for (var i = 0; i < x; i++)
    clearTimeout(x);

However, this peace of code did not work on Google Chrome. So I made improvement for this:

var x = setTimeout('alert("x");',100000); //It is very low probability that after 100 seconds x timeout will not be cleared
for (var i = 0; i <= x; i++)
    clearTimeout(i);

Finally, it is a hack, as it was mentioned in the comment above, so use it carefully.

Edit: fixed wrong variable used in loop (use i instead of x)

Abu Junayd
  • 115
  • 2
  • 7
4

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
  • 4,242
  • 1
  • 20
  • 20
1

you can stop setTimeout() of one function in other function using clearTimeout()

var myVar;

function myFunction() {
  myVar = setTimeout(function(){ alert("Hello"); }, 3000);
}

function myStopFunction() {
  clearTimeout(myVar);
}
Billu
  • 2,733
  • 26
  • 47