javascript - 倒数到零并停在零
问题描述
我从一分钟开始倒计时,希望它停在零。我不确定我的逻辑是否正确,因为这个数字超过了 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”,但它会通过它并变成负数。
解决方案
推荐阅读
- java - 使用 Spring JPA 在 Mysql 中搜索多个字段
- mysql - 选择带有两个 IN(列表)参数的查询
- cvs - CVS 提交不起作用并且不提供任何反馈
- c# - 删除 WPF FlowDocument 左填充?
- javascript - 需要帮助才能在 Jquery 中使用 .append() 到 div 元素
- bayesian - 具有三个层次结构的多级 stan 模型
- git - Github 没有在网站上显示分支
- json - POST 中的 JSON 正文使用 Rust reqwest Crate
- rest - GET List 和 GET Paginated 的 REST 命名约定
- javascript - 如何通过单击 Javascript 的按钮来模拟按键?