这是我的代码:
var hour = 00
var minute = 00
var second = 00
var milisecond = 00
function hour1() {
var hour = hour + 1;
document.getElementById("hour").innerHTML = hour1;
}
window.setInterval(hour1, 1000);
<div id="container">
<div id="hour">
<h1>00:</h1>
</div>
<div id="minute">
<h1>00:</h1>
</div>
<div id="second">
<h1>00</h1>
</div>
<div id="milisecond">
<h3>00</h3>
</div>
</div>
当我运行时,它说:
function hour1() { var hour = hour + 1; document.getElementById("hour").innerHTML = hour1; }
我该如何解决?
我试图使变量每分钟增加1,第二,等等,但它不起作用。这是我第一次尝试使用JavaScript实现一些东西,抱歉,如果我遗漏了一些非常简单的东西
答案 0 :(得分:0)
如果您需要时钟,可以使用类似的东西。
function setTime() {
var time = new Date();
var hours = time.getHours();
if (hours < 10) {
hours = "0" + hours;
}
var minutes = time.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
var seconds = time.getSeconds();
if (seconds < 10) {
seconds = "0" + seconds;
}
document.getElementById("timer").innerHTML = hours + " : " + minutes + " : " + seconds;
}
timer = setInterval(setTime, 1000);
<div id="timer"></div>