2009-11-25 11 views
28

que necesito para salir de un intervalo de funcionamiento si las condiciones son correctas:Cómo salida de setInterval

var refreshId = setInterval(function() { 
     var properID = CheckReload(); 
     if (properID > 0) { 
      <--- exit from the loop---> 
     } 
    }, 10000); 

Respuesta

80

Uso clearInterval:

var refreshId = setInterval(function() { 
    var properID = CheckReload(); 
    if (properID > 0) { 
    clearInterval(refreshId); 
    } 
}, 10000); 
+1

Cómo hacer 'clearInterval' desde fuera del bucle? – TharinduLucky

+1

simplemente llame a 'clearInterval (intervalName)', por ejemplo: 'var helloEverySecond = setInterval (function() {console.log (" hello ");}, 1000);' puede ser detenido por 'clearInterval (helloEverySecond);' –