stop loop ejemplo javascript setinterval

javascript - loop - ¿Cómo puedo borrarInterval() para todo setInterval()?



sleep javascript (8)

Este puede ser uno de lógica para borrar todo intervalo ...

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

Tengo un setInterval () llamado en un plugin de jQuery, pero quiero borrarlo de mi página principal, donde no tengo acceso a la variable en la que se almacenó setInterval.

¿Hay alguna manera de borrar todos los temporizadores presentes en una página?


Esto funcionó para mí.

//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); }


Estoy almacenando cada ID de intervalo en un contenedor oculto y llamo a esta función para recorrer y eliminar cada ID con 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''); }

Es feo, pero para mi propósito, funciona.


La forma en que lo he logrado es tener una matriz de nivel de aplicación (por ejemplo, Application.setIntervalIds = []) a la que presiono los ID de setInterval cada vez que se crea una. Entonces, simplemente puedo llamar a window.clearInterval (id) en cada identificación de la matriz cuando sea necesario.

Como ejemplo, cuando creo un setInterval nuevo escribo algo como (coffeescript):

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

Y luego tengo una función clearAllSetIntervals a la que puedo llamar cuando sea necesario:

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


La mejor manera que encontré ...

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( );


La respuesta

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

era el que estaba buscando. Con un poco de mejora de esta lógica tan simple, pude hacer algo como esto.

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]); } }


No, no puedes, no sin la variable original.


Puede anular 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. }