我似乎在计分板上发现了一个小问题。例如,如果我将其设置为恰好1分钟(1:00),它将执行1:0-1,然后执行0:59。我不太明白为什么要这么做,这是我的代码:
var minutes = 0;
var c = 0;
var secondes = 0;
var commence = setInterval(commencer, 1000);
clearInterval(commence);
function commencer(){
secondes -= 1;
document.getElementById('temps').innerHTML = minutes + ":" + secondes;
if (secondes < 10) {
document.getElementById('temps').innerHTML = minutes + ":" + c + secondes;
}
if (secondes <= 0) {
secondes = 60;
minutes = minutes - 1;
if (minutes < 0) {
clearInterval(commence);
minutes = 0;
secondes = 0;
clique = 0;
}
}
}
最佳答案
因为那就是你告诉它要做的。
您以1分钟0秒开始(实际上您没有,但是我们假设您的代码已更改为匹配您的问题)。
然后减去一秒钟,得到1分钟-1秒
您将此内容写到了HTML
然后您执行算术处理下溢,以得到0分59秒
一秒钟后,重复
更改顺序,它将按您的预期工作:
// Changed minutes from 0 to 1
var minutes = 1;
var c = 0;
var secondes = 0;
var commence = setInterval(commencer, 1000);
// Removed "clearInterval" call that stops the
// whole thing from running
function commencer() {
secondes -= 1;
// This has moved up
if (secondes <= 0) {
secondes = 60;
minutes = minutes - 1;
if (minutes < 0) {
clearInterval(commence);
minutes = 0;
secondes = 0;
clique = 0;
}
}
// This has moved down
document.getElementById('temps').innerHTML = minutes + ":" + secondes;
if (secondes < 10) {
document.getElementById('temps').innerHTML = minutes + ":" + c + secondes;
}
}
<span id="temps"></span>
您的新问题是下溢检查器将秒设置为60,因此您最终使用
0:60
。我怀疑这是您的意图。我现在将
if (secondes <= 0)
更改为if (secondes < 0)
,并将secondes = 60
更改为secondes = 59
,尽管我尚未验证这是正确的。我将其留给您练习。