質問

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