Matt
Matt

Reputation:

How can I clearInterval() for all setInterval()?

I've got a setInterval() called in a jQuery plugin, but I want to clear it from my main page, where I don't have access to the variable that the setInterval was stored in.

Is there a way to clear all timers present on a page?

Upvotes: 51

Views: 64656

Answers (9)

dot_pbf
dot_pbf

Reputation: 1

Inserting this code will allow the use of clearAllInterval();. When clearAllInterval(); is executed, clearInterval is applied to all running setIntervals.

window.intervallib=[];
    window.clearAllInterval=function(){
        for(i=0;i<window.intervallib.length;i++){
        clearInterval(window.intervallib[i]);
        }
        window.intervallib=[];
    }
    window.oldSetInterval = window.setInterval;
    window.setInterval = function(func, interval) {
        var id=oldSetInterval(func, interval)
        window.intervallib.push(id);
        return id;
    }

Upvotes: 0

Salman Ullah Khan
Salman Ullah Khan

Reputation: 728

This worked for me.

//Setting interval
var startInterval = setInterval(function () {
 //interval code here
}, 1000);

//Clearing interval
var countInterval = startInterval != undefined ? startInterval : 0;

 for (var a = 0; a < countInterval; a++) {
  clearInterval(a);
 }

Upvotes: 1

Hastig Zusammenstellen
Hastig Zusammenstellen

Reputation: 4440

I'm storing each interval id in a hidden container then calling this function to loop through and remove each id with window.clearInterval..

function clearAllIntervals() {
    $('.intervals-holder span').each(function() {
        var clearThis = parseInt($(this).text(), 10); // gets the interval id from the span holder
        window.clearInterval(clearThis); // removes the interval
        $(this).remove(); // removes the span holding the id
    })
}

// setting interval
// (i clear all first because in my code the buttons can be double clicked..
// ..and there is more than one button to start what i want to be started)
function audioRingingInterval() {
    clearAllIntervals();
    var intervalId = setInterval(audioRingingStartFunction, 500);
    $('.intervals-holder').append('<span>'+intervalId+'</span>');
}
// i call this when i want to set the interval and it calls the interval function above
function audioRingingStartFunction() {
    $('.audio-blinking').toggleClass('blinking');
}
// i call this when i want to stop the interval
function audioRingingStopFunction() {
    clearAllIntervals();
    $('.audio-blinking').removeClass('blinking');
}

It's ugly but for my purposes it works.

Upvotes: 1

Ashwini Jindal
Ashwini Jindal

Reputation: 831

Best way i found ...

var clearAllIntervals = function ( ) {

    var intervals = [];

    $(".elements").each(function() {
        intervals.push( setInterval(function() {

        }, 1000) );
    });

    return function clearAll ( ) {
        intervals.forEach( clearInterval );
    }

}( );

// When you want to clear them:
clearAllIntervals( );

Upvotes: 4

Marcus Ford
Marcus Ford

Reputation: 81

The answer

for (var i = 1; i < 99999; i++)
     window.clearInterval(i);

was the one I was looking for. With a little improvement of this very simple logic, I was able to do something like this.

var i = 0;
var rotatorId;
var rotator;

rotator =  setInterval(function() {myfunction(), 3000});
rotatorId[i] = rotator;
i++;

if (rotator > 1) {
   for(i = 1; i < rotatorId.length; i++){
      clearInterval(rotatorId[i]);                      
    }                       
}

Upvotes: 8

ProfNimrod
ProfNimrod

Reputation: 4310

The way I have achieved this is by having an application level array (e.g., Application.setIntervalIds = []) to which I push the setInterval ids to whenever one is created. Then I can simply call window.clearInterval(id) on each id in the array when I need to.

As an example, when I create a new setInterval I write something like (coffeescript):

id = setInterval (() -> function2call()), timeout
Application.setIntervalIds.push id

And then I have a clearAllSetIntervals function I can call when needed:

Application.clearAllSetIntervals = () ->
    $.each Application.setIntervalIds, (index, id) ->
         window.clearInterval id

Upvotes: 5

Isaac Waller
Isaac Waller

Reputation: 32750

You can override setInterval:

window.oldSetInterval = window.setInterval;
window.setInterval = function(func, interval) {
    var interval = oldSetInterval(func, interval);
    // store it in a array for stopping? stop it now? the power is yours.
}

Upvotes: 26

Vinay
Vinay

Reputation: 2594

This can be one of logic to clear all interval...

for (var i = 1; i < 99999; i++)
        window.clearInterval(i);

Upvotes: 68

Sasha Chedygov
Sasha Chedygov

Reputation: 130967

No, you can't, not without the original variable.

Upvotes: 10

Related Questions