<html>
<head>
<script>
function updateClock() {
var time = now.getHours() + ':' + now.getMinutes(),
document.getElementById('current').innerHTML = time;
setTimeout(updateClock, 1000);
}
</script>
</head>
<body onload="updateClock()">
<p id="current"> </p>
</body>
</html>
上面是实时动态更新的代码,它没有在浏览器上显示任何内容。非常感谢帮助
谢谢。
答案 0 :(得分:0)
updateClock中存在一些错误:
function updateClock() {
var now = new Date()
var time = now.getHours() + ':' + now.getMinutes();
document.getElementById('current').innerHTML = time;
setTimeout(updateClock, 1000);
}
now
必须是Date对象。
答案 1 :(得分:0)
now
需要成为约会对象我建议使用
*填充
* setInterval
* window.onload
代替body onload
我添加秒来显示更新。如果您只需要小时和分钟,请将间隔时间更改为10000左右。
function pad(num) {
return String("0"+num).slice(-2);
}
function updateClock() {
var now = new Date();
var time = pad(now.getHours()) + ':' + pad(now.getMinutes()) + ":" + pad(now.getSeconds());
document.getElementById('current').innerHTML = time;
}
window.onload = function() {
setInterval(updateClock, 500); // use a shorter interval for better update
}
<p id="current"></p>
答案 2 :(得分:0)
使用Js显示时间:
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('current').innerHTML =
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;
}
&#13;
<body onload="startTime()">
<p id="current">Time loading..</p>
</body>
&#13;
OR
您的代码已编辑:
function updateClock() {
var now = new Date();
var time = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
document.getElementById('current').innerHTML = time;
setTimeout(updateClock, 1000);
}
&#13;
<body onload="updateClock()">
<p id="current"></p>
</body>
&#13;
您遗失了var now = new Date();
和now.getSeconds();
。