我正在开发一个网页,其中包括一个数字时钟和日期显示.然而,我遇到了一个问题,即数字时钟不会动态更新;它只在页面刷新时更新.下面是我正在使用的相关JavaScript代码:
function updateDigitalClock() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
hours = (hours < 10) ? '0' + hours : hours;
minutes = (minutes < 10) ? '0' + minutes : minutes;
seconds = (seconds < 10) ? '0' + seconds : seconds;
var digitalClock = document.getElementById('digital-clock');
digitalClock.innerHTML = hours + ':' + minutes + ':' + seconds;
}
function updateDate() {
var now = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var dateElement = document.getElementById('date');
dateElement.innerHTML = now.toLocaleDateString('en-US', options);
}
有人能帮我确定为什么数字时钟没有动态更新,并提出解决方案吗?