2009-11-25 40 views
28

退出,我需要從一個運行區間,如果退出的條件是正確的:如何從setInterval的

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

回答

80

使用clearInterval

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

如何從外循環做'clearInterval'? – TharinduLucky

+1

只調用'clearInterval(intervalName)',例如:'var helloEverySecond = setInterval(function(){console.log(「hello」);},1000);''可以通過'clearInterval(helloEverySecond) –