0

I know how to stop a setInterval the normal way:

var intervalId = setInterval(myFunction, 1000);
clearInterval(intervalId);

What if I didn't save a reference to the intervalID? Is there any way to look up all the intervalIDs? Or clear all intervals?

at.
  • 50,922
  • 104
  • 292
  • 461
  • Don't! Just do it the proper way. You'll thank yourself later. The existing methods for it are educated guessing at best or brute force. None of which can ensure that some node.js based browser isn't using them internally for necessary tasks (though I don't think any major browser does this) If you have some running process that you are working on, you may be able to use your browser's developer tools to cancel it. – technosaurus Mar 23 '15 at 08:16
  • Was hoping for a better answer... but I guess I just shouldn't allow this situation to occur – at. Mar 23 '15 at 09:09

1 Answers1

0

The only way if you don't know the id is by brute forcing it.

for(var c=0;c<10000;c++) {
   window.clearInterval(c);
}

When possible, save the interval id. If using an external libary that implements intervals wihtout references, override those functions after loaded. This brute force method is not adviced to do because its time consuming, and has no guarantee that it will work.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Rather than using 10000 (end), you can set (and clear) a dummy interval and use that as the max. Then you can set the 0 (start) to that number for later usage ... assuming you are forced to use some library that couldn't just be fixed. You could even add a whitelist for known intervals that you don't want to kill. – technosaurus Mar 23 '15 at 09:46
  • That won't work. Different browsers implement seemingly random numbers. It is not always a sequential order. Just look at this example. http://jsfiddle.net/tLc99quv/1/ The longer a page runs, the more random numbers seem to get. And after a GC round interval counter sometimes even gets reset. I've seen it happen. – Tschallacka Mar 23 '15 at 09:56