【发布时间】:2016-03-08 16:04:06
【问题描述】:
我正在尝试仅在此网站上显示日期和时间,现在您必须刷新页面才能更新它(秒数等)。如何让秒自动更新?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Testbed</title>
</head>
<body>
<h1>
Javascript will find out the date and the browser.
</h1>
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var suffix = "AM";
if (seconds < 10)
seconds = "0" + seconds;
if (minutes < 10)
minutes = "0" + minutes;
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
document.write("<b><div id='d1'>" + hours + ":" +
minutes + ":"+ seconds + " " + suffix + "</div></b>");
</script>
</body>
</html>
【问题讨论】:
-
使用
setInterval定期调用处理程序...
标签: javascript html time web