我正在学习一些JavaScript并尝试使用简单的倒数计时器。如果我在timedCount()函数中插入checkTime(),则倒计时失败。我希望脚本在“警告”中显示一条消息。 div,当剩余的秒数(var c)是给定的整数(在这种情况下为100秒)。当我插入checkTime()函数时,脚本显示100并停止。当我将函数放在timeCount()函数中时,它也是这样。我很难过!
var c = 120;
var t;
var timer_is_on = 0;
start.onclick = startCount;
reset.onclick = resetCount;
function timedCount() {
document.getElementById("clock").innerHTML = c;
c = c - 1;
checkTime();
t = setTimeout(function() {
timedCount()
}, 1000);
}
function startCount() {
if (!timer_is_on) {
timer_is_on = 1;
timedCount();
document.getElementById('start').style.display = 'none';
document.getElementById('reset').style.display = 'none';
document.getElementById('stop').style.display = 'block';
}
}
function stopCount() {
clearTimeout(t);
timer_is_on = 0;
document.getElementById('start').style.display = 'block';
document.getElementById("start").innerHTML = "Resume";
document.getElementById('reset').style.display = 'block';
document.getElementById('stop').style.display = 'none';
}
function resetCount() {
clearTimeout(t);
c = 0;
timer_is_on = 0;
document.getElementById("txt").value = 0;
document.getElementById('start').style.display = 'block';
document.getElementById("start").innerHTML = "Start!";
}
function checkTime() {
if (c = 100) {
document.getElementById("warning").innerHTML = "Time nearly up!";
}
}

<div style="font-size:30px;"><span id="clock">0</span>
<span>:seconds</span></div>
<div id="warning"></div>
<button id="start">Start count!</button>
<button id="stop" onclick="stopCount()">Pause!</button>
<button id="reset">Reset!</button>
&#13;
答案 0 :(得分:0)
(c = 100)应该是(c === 100),你试图设置何时检查是否相等。