How to stop all timeouts and intervals using javascript? [duplicate]

Asked
Active3 hr before
Viewed126 times

7 Answers

intervals
90%

3 Worked great on Chrome to disable annoying reloads that made me lose my reading position. – haridsv Mar 1 '15 at 7:47 , 2 this 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 Cool, code also clears up all intervals. – Arsen Khachaturyan Feb 20 '15 at 7:04 ,Connect and share knowledge within a single location that is structured and easy to search.

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);
}
load more v
88%

javascript: Clear all timeouts? 10 answers ,https://www.onsip.com/blog/avoiding-javascript-settimeout-and-setinterval-problems,You cannot clear any timeouts and intervals you don't know about. ,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.

// 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
load more v
72%

Asynchronous JavaScript,Cooperative asynchronous JavaScript: Timeouts and intervals,Cooperative asynchronous Java​Script: Timeouts and intervals,Asynchronous JavaScript overview

let myGreeting = setTimeout(() => {
   alert('Hello, Mr. Universe!');
}, 2000);
load more v
65%

In Node.js timers return an object with no exposed properties or methods, except ref and unref. Is there still a way to clear all timers and intervals in Node.js?,If you'd like this, could you open a feature request on https://github.com/nodejs/node? Thanks!,Neither of these are pretty. It would be fantastic to have a better way to do this.,I'd rather not use process.exit since my script is consumed by other scripts, which manage their own process. It seems like there are a few workarounds:

window.setTimeout(() => {
   console.log('Timeout');
}, 500);
window.setInterval(() => {
   console.log('Interval');
}, 1000);

let id = window.setTimeout(() => {}, 0);
console.log(id);
while (id) {
   window.clearTimeout(id);
   id--;
}

id = window.setInterval(() => {}, 0);
console.log(id);
while (id) {
   window.clearInterval(id);
   id--;
}
load more v
75%

Updated answer after reading the duplicate I closed this question with - ,It works and tested in Chrome on OSX, Why are parenthesis used to wrap a javascript function call? [duplicate] 3 年前 ,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

// 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
40%

var myVar;

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

function myStopFunction() {
   clearTimeout(myVar);
}
load more v
22%

They are not in the window object, but they have ids, which (afaik) are consecutive integers.,So you may clear all timeouts like so:,Is there a way to clear all time outs from a given window? I suppose the timeouts are stored somewhere in the window object but couldn't confirm that.,You can use it to clear all timers in the current window:

So you may clear all timeouts like so:

var id = window.setTimeout(function() {}, 0);

while (id--) {
   window.clearTimeout(id); // will do nothing if no timeout with id is present
}
load more v