为什么在输出中递减后不显示零?是超时程序
Why zero is not shown after decrementing in the output?It is timeout program
这是我的代码 link http://codepen.io/meow414/pen/adLoEY 请有一个 look.The sec 变量在 sec=0 时不显示 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--;
}
}
这是我的代码 link http://codepen.io/meow414/pen/adLoEY 请有一个 look.The sec 变量在 sec=0 时不显示 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--;
}
}