gpt4 book ai didi

javascript - 为什么输出中递减后不显示零?这是超时程序

转载 作者:行者123 更新时间:2023-12-02 15:11:04 24 4
gpt4 key购买 nike

这是我的代码的链接 http://codepen.io/meow414/pen/adLoEY请看一下。当 sec=0 时,sec 变量不显示 0,它在 sec==0 时执行,但递减后不显示 0?如何显示它。

var min = 0;
var sec = 3;
var over;

function xyz() {
document.getElementById("abc").innerHTML = min + ":" + sec;

sec--; //it is getting decremeted upto 0,that's why below while loop is running bu in output zero is not shown for ex 3 after decrementing will show like 3 then 2 then 1 then it will chnage to 9.

while(sec == 0) {
sec = 9;
}
}

$("#start").click(function() {
over=setInterval(xyz, 1000);
});

$("#pause").click(function() {
clearInterval(over);
});

$("#reset").click(function() {
min = 0;
sec = 5;
clearInterval(over);
document.getElementById("abc").innerHTML = min + ":" + sec;
});

最佳答案

您的函数使用 while 作为条件(而不是 if)。

此外,当您将 sec 从 1 更改为 0 时,您会根据该条件立即将其设置为 9。

尝试

 function xyz() {
// Show sec, even if it's 0
document.getElementById("abc").innerHTML = min + ":" + sec;
// Then if(sec===0) set sec to 9 (the HTML will still show 0)
if (sec === 0) {
sec = 9;
}
// Otherwise, decrement sec
else {
sec--;
}
}

代码笔:http://codepen.io/anon/pen/rxGBbd

关于javascript - 为什么输出中递减后不显示零?这是超时程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com