clearInterval does not stop goes passed 0 (countdown)
-
24-06-2021 - |
문제
This is just a simple setInterval example I am working with, but the clearInterval is not stopping the setInterval. It counts passed 0 to negitive numbers. How do I make this work and stop at 0?
getMoreVideosCountdown = 10;
getMoreVideos = setInterval(function() {
console.log(getMoreVideosCountdown);
if (getMoreVideos <= 0) {
clearInterval(getMoreVideos);
}
getMoreVideosCountdown--;
},1000);
해결책
if (getMoreVideos <= 0) {
should be
if (getMoreVideosCountdown <= 0) {
Currently, you're checking if the interval identifier
gets lower or equals zero, which is never going to happen.
제휴하지 않습니다 StackOverflow