Search code examples
javascriptstopwatchclearinterval

Failing to use clear interval method for a stopwatch in js


I'm trying to create a stopwatch for a running game (starts when first card is clicked, stops when all cards are open) and unlike setInterval which works as should be, I can't get clearInterval to work.

var elPrevCard = null;
var flippedCards = 0;
let elNewGameBtn = document.querySelector('.new-game');
let elAllCards = document.querySelectorAll('.card');
var TOTAL_COUPLES_COUNT = 3;

let elStopwatch = document.querySelector('.stopwatch');
let ms = 0;
let sec = 0;
let min = 0;
let time;

function stopwatchTime() {
    ms++;
    if (ms >= 100) {
        sec++;
        ms = 0;
    }
    
    if (sec === 60) {
        min++;
        sec = 0;
    }
    
    if (min === 60) {
        ms, sec, min = 0;
    }
    
    let millis = ms < 10 ? `0` + ms : ms;
    let seconds = sec < 10 ? `0` + sec : sec;
    let minute = min < 10 ? `0` + min : min;
    
    let timer = `${minute}:${seconds}:${millis}`;
    elStopwatch.innerHTML = timer;
};

function startStopwatch() {
    time = setInterval(stopwatchTime, 10);
}

function stopStopwatch() {
    clearInterval(time);
}

function resetStopwatch() {
    ms = 0;
    sec = 0;
    min = 0;
    
    elStopwatch.innerHTML = `00:00:00`;
}


for (let i = 0; i < elAllCards.length; i++) {
    elAllCards[i].addEventListener('click', startStopwatch();
    });
}

function cardClick(elCard) {
    elCard.classList.add('flipped');
    
    if (elPrevCard === null) {
        elPrevCard = elCard;
    } else {
        var card1 = elPrevCard.getAttribute('data-card');
        var card2 = elCard.getAttribute('data-card');
        
        if (card1 !== card2) {
            setTimeout(function() {
                elCard.classList.remove('flipped');
                elPrevCard.classList.remove('flipped');
                elPrevCard = null;
            }, 1000);
            
        } else {
            flippedCards++;
            elPrevCard = null;
            
            if (TOTAL_COUPLES_COUNT === flippedCards) {
                stopStopwatch();

                elNewGameBtn.style.display = 'inline';
            }
        }
    }
}

elNewGameBtn.addEventListener('click', function newGame() {
    for (let i = 0; i < elAllCards.length; i++) {
        elAllCards[i].classList.remove('flipped');
    }
    
    flippedCards = 0;
    elNewGameBtn.style.display = 'none';
    resetStopwatch();
});

Would very much appreciate help with this, I can't tell what I'm missing here.

Thanks in advance to all helpers!


Solution

  • You can simply check whether time is currently holding a value, and set it to null when you clear the interval.

    Note: Also, in adding the listeners, be sure to pass the function not call it. so card.addEventListener('click', startStopwatch); not card.addEventListener('click', startStopwatch());

    let time = null;
    
    // ...
    
    function startStopwatch() {
      if (time === null) {
        time = setInterval(stopwatchTime, 10);
      }
    }
    
    function stopStopwatch() {
      if (time !== null) {
        clearInterval(time);
        time = null;
      }
    }
    
    //...
    
    for (const card of elAllCards) {
      card.addEventListener('click', startStopwatch);
    }