Is there a way to find if any intervals (set by window.setInterval) are still open? I just want to have a way to double check my code ( in debugging) and be sure I’m not leaving any unclosed.
@6tr6trauthorJul 27.2010 — #LOL, uh, yeah I know that. What I'm asking is if there's a function I can call that will return all intervals that have been accidentally left open (e.g. if I'd created them dynamically and my code has a bug that didn't close some).
@Declan1991Jul 28.2010 — #Not unless you keep track of them yourself. If you want to be absolutely sure, you could write your own setInterval and clearInterval interface that does something like this. However, I wonder is it setTimeout you need, or how exactly you could forget to stop setIntervals. I think you might be better off to ask us what your problem is, not what you think a solution could be.window.openIntervals[window.openIntervals.length] = window.setInterval(...);
@Sterling_IsfineJul 28.2010 — #Is there a way to find if any intervals (set by window.setInterval) are still open? I just want to have a way to double check my code ( in debugging) and be sure I'm not leaving any unclosed.[/QUOTE] Here's a demonstration of an interval management object, which if used consistently should prevent the usual hazards:[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Interval Manager</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> </head> <body> <p> <script type='text/javascript'>
var IntervalManager = { /*
set : Set an interval using setInterval and store its ID IntervalManager.set( "uniqueID", function reference, milliseconds );
clear : Clear an interval and forget its ID IntervalManager.clear( "uniqueID" );
any : Detect if any intervals are active. Returns ID of first active or false. if( IntervalManager.any() ) {...}
clearAll : Clears all intervals whose IDs are stored and forgets their IDs IntervalManager.clearAll();
@mrhooJul 28.2010 — #You cannot reliably tell if there are any unnamed intervals running, but you [B]can [/B]shut down any that are open.
You can refer to an interval in a clearInterval call with the integer value returned when you set the interval.
The spec doesn't say what the first interval's integer is, but in practice, all the browsers assign a larger integer to each new timer.
You can find the 'next' integer by creating a new, dummy interval, and clear every integer less than the new one.
There is no error if you call clearInterval on an integer that is not set as an interval.
IE can start the integer numbering with an immense number, more than you want to loop through, but they still increase by one integer at a time.
You can figure that there couldn't be more than some defined number of unnamed intervals and quit after testing that limit.
[CODE]function quitIntervals(limit){ limit=limit || 1000; // could be 100 or 100000- your call var np, n= setInterval(function(){},100000); // not meant to ever run np= Math.max(0, n-limit); while(n> np){ clearInterval(n--); } }[/CODE]
@KxmodeMay 02.2019 — #Considering the age of this question and the fact that it came up while I sought an answer, I'll go ahead and post my solution for how I did this.
In Chrome's Inspector Tool, under the Sources tab, set a breakpoint inside the function called by the setInterval. When the script stops at the breakpoint expand the Call Stack accordion in the right panel. You'll notice several "setInterval (async)." The one you want to look for will have a link to your code that has the setInterval variable. If you see too many setIntervals, you can hide them by blackboxing the external script that created the setInterval.
So every time you press the "pause play" button in the upper-right corner of the Inspector, this loops back to the breakpoint. Every time this happens, you'll notice the line number of the setInterval changes and this is how you can track which one might have strayed, become erratic, or simply ignores your clearInterval.
Screenshot of the area in the Inspector for reference https://i.imgur.com/YU9Mupq.png