倒数到零并停止在零

时间:2019-04-26 15:27:48

标签: javascript setinterval clearinterval

我从一分钟开始倒计时,希望它从零开始停顿。我不确定我的逻辑是否正确,因为数字正超过0变成负数。

我尝试清除间隔(clearInterval(interval)),但似乎不起作用。

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);
}

我希望输出在“ 00:00”停止,但它传递给它并变为负数。

0 个答案:

没有答案