I'm working on an ajax web appliation which contains many running timeouts and intervals. And now I need to clear all running timeouts and intervals sometimes. Is there a simple way to stop everything without need to store every timeout and interval ID and iterate through them and clear them?
-
Related: listing them: http://stackoverflow.com/questions/858619/viewing-all-the-timouts-intervals-in-javascript – Ciro Santilli OurBigBook.com Jan 06 '16 at 20:34
-
Actually Solved: http://stackoverflow.com/questions/8860188/is-there-a-way-to-clear-all-time-outs Because typically timeout id's are stored in increasing order and start from 0 or 1. There may be gaps. But you can do a... "push-down stack" to iterate through all possible timers. – RoboticRenaissance Jan 21 '17 at 15:04
-
I closed this using the better, newer answer you posted – mplungjan Jan 21 '17 at 16:24
-
Do note that the question included _without storing and iterate through them_ - the answer is no to that question. There is no way to call a global clearAllIntervals without iterating over stored IDs – mplungjan Jan 21 '17 at 16:26
10 Answers
Sometimes it's possible to save the timer Id / Handle to clear it later which would be the best solution. So this is a second best. But I wanted to give a better understanding of what's going on. It basically 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!
It is a little hackish, so be warned!
// Set a fake timeout to get the highest timeout id
var highestTimeoutId = setTimeout(";");
for (var i = 0 ; i < highestTimeoutId ; i++) {
clearTimeout(i);
}

- 17,368
- 20
- 81
- 90
-
So this would run over 8digits in IE - that is potentially 100 million times. Ps: same answer as the less commented one by DOD – mplungjan Mar 16 '13 at 08:54
-
2this code helped to kill all timers on a webpage that i was trying to play with. I ran it from console. – Penuel Jul 24 '13 at 10:41
-
2
-
3Worked great on Chrome to disable annoying reloads that made me lose my reading position. – haridsv Mar 01 '15 at 07:47
-
2Could be made into a bookmarklet --- `javascript:var highmark=setTimeout(function(){while(highmark>0)clearTimeout(highmark--)})+100` – Refael Ackermann Jul 18 '16 at 17:27
-
I was not aware that it will return higher number always than previous. – shyammakwana.me Aug 12 '16 at 09:45
-
4The spec says that the id returned just needs to be a non zero value. The fact that it's sequential appears to have no grounding from an authoritative source. So as long as "the powers that be" keep it that way, this *hack* will work – SeanDowney Aug 15 '16 at 22:13
Updated answer after reading the duplicate I closed this question with -
It works and tested in Chrome on OSX
// run something
var id1 = setInterval(function() { console.log("interval", new Date())}, 1000);
var id2 = setTimeout(function() { console.log("timeout 1", new Date())}, 2000);
var id3 = setTimeout(function() { console.log("timeout 2", new Date())}, 5000); // not run
setTimeout(function() { console.log("timeout 3", new Date())}, 6000); // not run
// this will kill all intervals and timeouts too in 3 seconds.
// Change 3000 to anything larger than 10
var killId = setTimeout(function() {
for (var i = killId; i > 0; i--) clearInterval(i)
}, 3000);
console.log(id1, id2, id3, killId); // the IDs set by the function I used
NOTE: Looked at window objects that had a typeof number - funnily enough IE assigns an 8 digit number, FF a single digit starting with 2

- 169,008
- 28
- 173
- 236
-
Tried to find window objects that had a typeof number - funnily enough IE assigns an 8 digit number, FF a single digit starting with 2 – mplungjan Jun 29 '10 at 13:42
-
I think that a window object containing intervals would be an array containing objects, and that x and y would be the id of the new objects in that array. But Interval and Timeout id's are typically stored densely in increasing order from 0 or 1. Some may start higher. So you would go from y to 0 calling `clearInterval()` on each number. I posted a link to that answer in my comment on the Question above. But... not only does your 'answer' not answer the question, it also goes about it in a weird, backwards way that doesn't show thought on how objects and arrays work. – RoboticRenaissance Jan 21 '17 at 15:13
-
Agreed. Please note the date too. It is quite old. Also I was never an OO programmer. Also note the 8 digits you want to run over in IE – mplungjan Jan 21 '17 at 16:15
-
A little fanagling there can fix that. Instead of while( i--), you could do while (i>9999999) if you're in ie. Or, if you're only running this once to stop spam as you get at other things not available later, the second it would take to run clearInterval (x) a million more times would be worth it. – RoboticRenaissance Jan 21 '17 at 16:31
-
-
1
-
1
Here is a workaround.
window.timeoutList = new Array();
window.intervalList = new Array();
window.oldSetTimeout = window.setTimeout;
window.oldSetInterval = window.setInterval;
window.oldClearTimeout = window.clearTimeout;
window.oldClearInterval = window.clearInterval;
window.setTimeout = function(code, delay) {
var retval = window.oldSetTimeout(code, delay);
window.timeoutList.push(retval);
return retval;
};
window.clearTimeout = function(id) {
var ind = window.timeoutList.indexOf(id);
if(ind >= 0) {
window.timeoutList.splice(ind, 1);
}
var retval = window.oldClearTimeout(id);
return retval;
};
window.setInterval = function(code, delay) {
var retval = window.oldSetInterval(code, delay);
window.intervalList.push(retval);
return retval;
};
window.clearInterval = function(id) {
var ind = window.intervalList.indexOf(id);
if(ind >= 0) {
window.intervalList.splice(ind, 1);
}
var retval = window.oldClearInterval(id);
return retval;
};
window.clearAllTimeouts = function() {
for(var i in window.timeoutList) {
window.oldClearTimeout(window.timeoutList[i]);
}
window.timeoutList = new Array();
};
window.clearAllIntervals = function() {
for(var i in window.intervalList) {
window.oldClearInterval(window.intervalList[i]);
}
window.intervalList = new Array();
};
It works for set/clear timeout/interval functions called after these lines are executed. Try and see it works:
setInterval('console.log(\'a\')', 1000);
setInterval('console.log(\'b\')', 500);
setInterval('console.log(\'c\')', 750);
setTimeout('clearAllIntervals()', 10000);
Proxying does the magic.

- 197
- 1
- 3
-
1
-
Plus one. I'd have found this answer more easily if had included the phrase "monkey patch", so there it is. – RichieHindle Jun 18 '15 at 09:14
var noofTimeOuts = setTimeout('');
for (var i = 0 ; i < noofTimeOuts ; i++) clearTimeout(i);

- 44,509
- 17
- 89
- 111

- 149
- 2
- 2
var max = setTimeout(function(){ /* Empty function */ },1);
for (var i = 1; i <= max ; i++) {
window.clearInterval(i);
window.clearTimeout(i);
if(window.mozCancelAnimationFrame)window.mozCancelAnimationFrame(i); // Firefox
}

- 379
- 5
- 6
-
1
-
1yes, you can get the max id : var max = setTimeout(()=>{},1) for (var i = 1; i <= max ; i++) { window.clearInterval(i); window.clearTimeout(i); if(window.mozCancelAnimationFrame) window.mozCancelAnimationFrame(i); // Firefox } – boubkhaled Mar 05 '18 at 11:31
There's nothing built-in, but it's pretty easy to blast through all currently outstanding deferred execution functions by calling this clearAll()
function:
function clearAll() {
for (var i = setTimeout(function() {}, 0); i > 0; i--) {
window.clearInterval(i);
window.clearTimeout(i);
if (window.cancelAnimationFrame) window.cancelAnimationFrame(i);
}
}
If you are in charge of the page you run, and can wrap the native deferred execution functions in wrappers that do the house keeping for of course equip each setter function with a corresponding .clearAll()
too:
(function(deferFunctions) {
for (var setter in deferFunctions) (function(setter, clearer) {
var ids = [];
var startFn = window[setter];
var clearFn = window[clearer];
function clear(id) {
var index = ids.indexOf(id);
if (index !== -1) ids.splice(index, 1);
return clearFn.apply(window, arguments);
}
function set() {
var id = startFn.apply(window, arguments);
ids.push(id);
return id;
}
set.clearAll = function() { ids.slice(0).forEach(clear); };
if (startFn && clearFn) {
window[setter] = set;
window[clearer] = clear;
}
})(setter, deferFunctions[setter]);
})(
{ setTimeout: 'clearTimeout'
, setInterval: 'clearInterval'
, requestAnimationFrame: 'cancelAnimationFrame'
});
To try that it works, you could then try doing this, for instance, which will remain silent, as none of the callbacks end up firing before they're cancelled again:
// Some example timers of all types:
requestAnimationFrame(console.error);
setInterval(console.info, 1000, 'interval');
setTimeout(alert, 0, 'timeout');
// Now you can clear all deferred functions
// by execution type, whenever you want to:
window.setTimeout.clearAll();
window.setInterval.clearAll();
window.requestAnimationFrame.clearAll();

- 5,030
- 2
- 44
- 66
A little hack added to Gokhan Ozturk's answer
If you are using third party libraries which uses Timeouts
and Intervals
then they will also be cleared, so I added one parameter to notify function that this interval is to be push
'ed or not to array.
window.setTimeout = function(code, delay, toBeAdded) {
var retval = window.oldSetTimeout(code, delay);
var toBeAdded = toBeAdded || false;
if(toBeAdded) {
window.timeoutList.push(retval);
}
return retval;
};
... // likewise for all functions.

- 1
- 1

- 5,562
- 2
- 29
- 50
You might be better off creating a scheduler. Take a look at this approach by Nader Zeid:
https://www.onsip.com/blog/avoiding-javascript-settimeout-and-setinterval-problems
It's an approach that help create some determinacy (because "the time interval argument of each of those functions really only establishes that the given function will execute after at least that amount of time. So a timed event can miss its target by literally any amount of time.").
Specifically, to the question you raise here, you can easily add and remove functions from the queue. While this response is long after the question was raised, hopefully it's helpful to any who find themselves struggling with Timeouts and Intervals.

- 11
- 2
You cannot clear any timeouts and intervals you don't know about.
You'd need something like getTimeoutList
which isn't in the DOM3 spec, or even planned, AFAIK.

- 90,639
- 22
- 233
- 295
The previous proxying trick is nice, but if you have a lot of timeouts and intervals, I would not fill the arrays with consecutive numbers [1,2,3....]
, but with intervals. For example, instead of having [1,2,3,7,8,9]
, you would have maybe something like ['1-3','7-9']
or [[1,3],[7,9]]
, as a memory optimization. Of course this trick is only suited if you have a lot of timeouts and intervals and also if you would not stop arbitrary intervals that often.

- 3,266
- 3
- 35
- 39

- 65
- 1
- 6