Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show all currently running setIntervals [duplicate]

I'm writing an application that utilizes JavaScript timeouts and intervals to update the page. Is there a way to see how many intervals are setup? I want to make sure that I'm not accidentally going to kill the browser by having hundreds of intervals setup.

Is this even an issue?

like image 975
Omar Kooheji Avatar asked May 13 '09 15:05

Omar Kooheji


5 Answers

I don't think there is a way to enumerate active timers, but you could override window.setTimeout and window.clearTimeout and replace them with your own implementations which do some tracking and then call the originals.

window.originalSetTimeout = window.setTimeout;
window.originalClearTimeout = window.clearTimeout;
window.activeTimers = 0;

window.setTimeout = function(func, delay) {
    window.activeTimers++;
    return window.originalSetTimeout(func, delay);
};

window.clearTimeout = function(timerID) {
    window.activeTimers--;
    window.originalClearTimeout(timerID);
};

Of course, you might not always call clearTimeout, but this would at least give you some way to track what is happening at runtime.

like image 189
Paul Dixon Avatar answered Oct 19 '22 19:10

Paul Dixon


I made a Chrome DevTools extension that shows all intervals. Cleared ones are greyed out.

Timers Chrome Devtool extension

setInterval-sniffer

like image 35
NVI Avatar answered Oct 19 '22 17:10

NVI


Instead of just have a count of timers, here is an implementation which stores all timerid's into an array. It only shows active timers while the accepted answer only counts calls to setTimeout & clearTimeout.

(function(w) {
    var oldST = w.setTimeout;
    var oldSI = w.setInterval;
    var oldCI = w.clearInterval;
    var timers = [];
    w.timers = timers;
    w.setTimeout = function(fn, delay) {
        var id = oldST(function() {
            fn && fn();
            removeTimer(id);
        }, delay);
        timers.push(id);
        return id;
    };
    w.setInterval = function(fn, delay) {
        var id = oldSI(fn, delay);
        timers.push(id);
        return id;
    };
    w.clearInterval = function(id) {
        oldCI(id);
        removeTimer(id);
    };
    w.clearTimeout = w.clearInterval;

    function removeTimer(id) {
        var index = timers.indexOf(id);
        if (index >= 0)
            timers.splice(index, 1);
    }
}(window));

This is how you can get the count of active timers on the page:

timers.length;

This is how you can remove all active timers:

for(var i = timers.length; i--;)
    clearInterval(timers[i]);

Known limitations:

  • You can only pass a function (not a string) to setTimeout with this monkey patch.
  • The function assumes clearInterval and clearTimeout do the same, which they do but it could change in the future.
like image 37
A1rPun Avatar answered Oct 19 '22 18:10

A1rPun


Seeing as Paul has only covered setTimeout I thought I would share a counter for setInterval/clearInterval.

window.originalSetInterval = window.setInterval;
window.originalClearInterval = window.clearInterval;
window.activeIntervals = 0;
window.setInterval = function (func, delay)
{
    if(func && delay){
            window.activeIntervals++;
    }
    return window.originalSetInterval(func,delay);
};
window.clearInterval = function (intervalId)
{
    // JQuery sometimes hands in true which doesn't count
    if(intervalId !== true){
        window.activeIntervals--;
    }
    return window.originalClearInterval(intervalId);
};
like image 41
crv Avatar answered Oct 19 '22 17:10

crv


We've just published a package solving this exact issue.

npm install time-events-manager

With that, you can view and manage them via timeoutCollection object (and javascript's intervals viaintervalCollection object).

timeoutCollection.getScheduled(); timeoutCollection.getCompleted(); timeoutCollection.getAll();

like image 5
Ziv polack Avatar answered Oct 19 '22 17:10

Ziv polack