我已经看了几个小时,似乎无法理解为什么我的代码在按下按钮时没有启动计时器倒计时。任何帮助将不胜感激,谢谢!
HTML
<div id="timerHours">00</div>
<div id="timerMins">00</div>
<div id="timerSecs">00</div>
<input type="button" value="Start" onclick="startTimer()"/>
JavaScript
var t;
var timerDefault = new Date();
timerDefault.setHours(0, 0, 0);
function setTimer(){
var element = document.getElementById("timerHours");
hours = parseFloat(element.innerHTML);
element = document.getElementById("timerMins");
minutes = parseFloat(element.innerHTML);
element = document.getElementById("timerSecs");
seconds = parseFloat(element.innerHTML);
timerDefault.setHours(hours, minutes, seconds);
}
function startTimer() {
setTimer();
var h = timerDefault.getHours();
var m = timerDefault.getMinutes();
var s = timerDefault.getSeconds();
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
document.getElementById("timerHours").innerHTML = h;
document.getElementById("timerMins").innerHTML = m;
document.getElementById("timerSecs").innerHTML = s;
timerDefault.setSeconds(timerDefault.getSeconds() - 1);
if (h == 0 && m == 0 && s == 0) clearTimeout(t)
t = setTimeout(startTimer, 1000);
}