Ответ 1
Используйте clearInterval:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
clearInterval(refreshId);
}
}, 10000);
Мне нужно выйти из интервала выполнения, если условия верны:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
<--- exit from the loop--->
}
}, 10000);
Используйте clearInterval:
var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
clearInterval(refreshId);
}
}, 10000);
Вы можете ограничить переменную, чтобы избежать загрязнения пространства имен:
const CheckReload = (() => {
let counter = - 5;
return () => {
counter++;
return counter;
};
})();
{
const refreshId = setInterval(
() => {
const properID = CheckReload();
console.log(properID);
if (properID > 0) {
clearInterval(refreshId);
}
},
100
);
}
Передайте значение setInterval
в clearInterval.
const interval = setInterval(() => {
clearInterval(interval);
}, 1000)
Таймер уменьшается каждую секунду, пока не достигнет 0.
let secondsToCountDown = 2
const interval = setInterval(() => {
// just for presentation
document.querySelector('.timer').innerHTML = secondsToCountDown
if (secondsToCountDown === 0) {
clearInterval(interval); // time is up
}
secondsToWait--;
}, 1000);
<p class="timer"></p>