这段代码有什么问题?它没有在页面上显示一个计时器,当我在JSFiddle中测试它时,我得到一个“输入结束”语法错误。
$( engage ).click(function () {
setTimeout(function () {
var timeDisplay = document.createElement("p");
clock.appendChild(timeDisplay);
while (true) {
var target = 50 * 60;
var curr = target;
var x = setInterval(function () {
curr -= 1;
var mins = Math.floor(curr / 60);
var secs = curr % 60;
if (secs < 10) {
secs = "0" + secs;
}
console.log(mins + ":" + secs);
timeDisplay.innerHTML = mins + ":" + secs;
}, 1000);
}
}, 500);
答案 0 :(得分:0)
好的,所以你做了以下,但我无法理解你想要做什么。我建议你重命名变量,让自己更好地理解代码。
$( engage ).click(function () {
setTimeout(function () {
var timeDisplay = document.createElement("p");
clock.appendChild(timeDisplay);
while (true) {
var target = 50 * 60;
var curr = target;
var x = setInterval(function () {
curr -= 1;
var mins = Math.floor(curr / 60);
var secs = curr % 60;
if (secs < 10) {
secs = "0" + secs;
}
console.log(mins + ":" + secs);
timeDisplay.innerHTML = mins + ":" + secs;
}, 1000);
}
}, 500);
我有以下工作代码:
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
console.log(h + ":" + m + ":" + s);
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}