我想创建时钟,其时间由服务器初始化,然后使用以下代码继续客户端:
//initializing by server(ASP.NET):
<% var now=System.DateTime.Now; %>
var h=<%=now.Hour %>;
var m=<%=now.Minute %>;
var s=<%=now.Second %>;
//then client:
function startTime() {
s++;
if (s == 60) {
s = 0;
m++;
if (m == 60) {
m = 0;
h++;
if (h == 24)
h = 0;
}
}
m = checkTime(m);
s = checkTime(s);
h = checkTime(h);
document.getElementById('clock').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function () { startTime() }, 1000);
}
function checkTime(i) {
if (i < 10) {
if(i.toString().length<2)
i = "0" + i;
}
return i;
}
window.load = startTime();
但是时钟每 10 分钟就会迟到 5 秒。
我怎样才能防止这种延迟?