0

I'm counting down from a minute and want it to stop at zero. I'm not sure if my logic on this is correct because the number is going past 0 into the negatives.

I've tried clearing the interval (clearInterval(interval)) but it doesn't seem to work.

var timer = [0,0,0]; //seconds,hundredths,thousandths
var counter = 10; //starts at 60s; 10s for testing purposes
var interval;
var timerRunning = false;

//countdown timer
function runTimer() {
    let currentTime = leadingZero(timer[0]) + ":" + leadingZero(timer[1]);
    theTimer.innerHTML = currentTime;

    timer[2]--; //counting down in thousandths increments

    timer[0] = (counter + Math.floor(timer[2]/100)); //" second increments
    timer[1] = Math.floor(timer[2] - (Math.floor(timer[2]/100) * 100)); //" hundredth increments

    if (timer == [0,0,0]) { 
      clearInterval(interval);
      timer = [0,0,0];
      timerRunning = false;

      theTimer.innerHTML = "00:00";
    }
}

// Start the timer:
function start() {
    let textEnterdLength = testArea.value.length;
    if (textEnterdLength === 0 && !timerRunning) {
        timerRunning = true;
        interval = setInterval(runTimer, 10);
    }
    //console.log(textEnterdLength);
}

I expect the output to stop at "00:00" but it goes passed it and into negative.

Justin
  • 133
  • 5

0 Answers0