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

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); 
}

Leave a Comment