Pergunta

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);
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top