2009-11-25 13 views
28

ho bisogno di uscire da un intervallo di esecuzione se le condizioni sono corrette:Come uscire da setInterval

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

risposta

80

Uso clearInterval:

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

Come fare 'clearInterval' dal di fuori del ciclo? – TharinduLucky

+1

chiama semplicemente 'clearInterval (intervalName)', ad esempio: 'var helloEverySecond = setInterval (function() {console.log (" ciao ");}, 1000);' può essere fermato da 'clearInterval (helloEverySecond);' –