【发布时间】:2009-11-25 06:53:58
【问题描述】:
如果条件正确,我需要退出一个运行区间:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
<--- exit from the loop--->
}
}, 10000);
【问题讨论】:
如果条件正确,我需要退出一个运行区间:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
<--- exit from the loop--->
}
}, 10000);
【问题讨论】:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
clearInterval(refreshId);
}
}, 10000);
【讨论】:
clearInterval(intervalName),例如:var helloEverySecond = setInterval(function() { console.log("hello");}, 1000);可以被clearInterval(helloEverySecond);停止
将setInterval 的值传递给clearInterval。
const interval = setInterval(() => {
clearInterval(interval);
}, 1000)
计时器每秒递减,直到达到 0。
let secondsRemaining = 10
const interval = setInterval(() => {
// just for presentation
document.querySelector('p').innerHTML = secondsRemaining
// time is up
if (secondsRemaining === 0) {
clearInterval(interval);
}
secondsRemaining--;
}, 1000);
<p></p>
【讨论】:
您可以限定变量以避免污染命名空间:
const CheckReload = (() => {
let counter = - 5;
return () => {
counter++;
return counter;
};
})();
{
const refreshId = setInterval(
() => {
const properID = CheckReload();
console.log(properID);
if (properID > 0) {
clearInterval(refreshId);
}
},
100
);
}
【讨论】: