Question

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);
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top