我正試圖在本網站上顯示日期和時間,現在您必須刷新頁面以便更新(秒等)。我如何自動更新秒數?自動更新秒數?
<!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'週期性的調用處理程序... – Rayon