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?
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?
This can be one of logic to clear all interval...
for (var i = 1; i < 99999; i++)
window.clearInterval(i);
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.
}
new function
instead of function
? –
Hypertrophy 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]);
}
}
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
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( );
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.
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);
}
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;
}
© 2022 - 2024 — McMap. All rights reserved.