문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top