/    Sign up×
Community /Pin to ProfileBookmark

Is there a way to find if any intervals are still open?

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.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@PadonakJul 27.2010 — every interval stays open until it is closed
Copy linkTweet thisAlerts:
@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).

For example, something along the lines of:

window.openIntervals
Copy linkTweet thisAlerts:
@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(...);
Copy linkTweet thisAlerts:
@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();

*/

intervals : [/*28432953637269707465726C61746976652E636F6D*/],

set : function( intervalID, funcRef, period )
{
if( !this.intervals[ intervalID ] )

this.intervals[ intervalID ] = setInterval( funcRef, period );
else
alert("Attempted to set " + intervalID + ' more than once.');
},

clear : function( id )
{
clearInterval( this.intervals[ id ] );

delete this.intervals[ id ];

},

clearAll : function()
{
var table = this.intervals;

for( var i in table )
{
clearInterval( table[ i ] );
delete table[ i ];

}

},

any : function()
{
var table = this.intervals, found = false;

for( var i in table )
if( table[ i ] !== null )
{
found = table[ i ];

break;

}

return found;
}

}

</script>


<script type='text/javascript'>

setInterval( function(){ document.title="___________" }, 995 ); /* Unmanaged interval */

</script>


<input type='button' onclick= 'IntervalManager.set( "t100", function(){ document.title="Run every 100" }, 100 );' value='Set 100'>
<input type='button' onclick= 'IntervalManager.set( "t250", function(){ document.title="Run every 250" }, 250 );' value='Set 250'>
<input type='button' onclick= 'IntervalManager.set( "t550", function(){ document.title="Run every 500" }, 550 );' value='Set 550'>
<br>
<input type='button' onclick='IntervalManager.clear( "t100" );' value='Clear 100'>
<input type='button' onclick='IntervalManager.clear( "t250" );' value='Clear 250'>
<input type='button' onclick='IntervalManager.clear( "t550" );' value='Clear 550'>
<br>
<input type='button' onclick='IntervalManager.clearAll();' value='Clear All'>
<input type='button' onclick='alert(IntervalManager.any())' value='Any active?'>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@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]
Copy linkTweet thisAlerts:
@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
×

Success!

Help @6tr6tr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...