以下代码为每个控制台生成一个滴答时钟。时钟应该通过<span class="displayWrapper"><input id="watch" class="column"></span>
函数显示在字段display
中。它拒绝这样做,我之前从未遇到过.val()方法的这个问题。就启动和运行而言,我对startWatch
函数的输入为1和0,因为正breakTime
会导致执行滞后(此时)。任何有关补救措施的想法都将受到赞赏。
function display(deadLine) {
let displayDate = new Date(deadLine);
let hrs = displayDate.getUTCHours().toString();
let min = displayDate.getUTCMinutes().toString();
let sec = displayDate.getUTCSeconds().toString();
let time = hrs.padStart(2, '0') + ' : ' + min.padStart(2, '0') + " : " + sec.padStart(2, '0');
$("#watch").val(time);
}
function startWatch(minutes, breakTime) {
breakHandler = setTimeout(function() {
let initialize = new Date(0);
let deadLine = new Date(minutes * 60000);
let currDate = Date.now();
display(deadLine);
tickHandler = setInterval(function() {
let timePass = Date.now() - currDate;
deadLine = deadLine - timePass;
if (deadLine > 0) {
let displayDate = new Date(deadLine);
display(displayDate);
currDate = Date.now();
console.log("currDate => " + currDate);
} else {
clearInterval(tickHandler);
startWatch(minutes, breakTime);
}
}, 1000);
}, breakTime);
}