可能重複:
Javascript to run the Clock (date and time) 4 times speeder在4倍速運行的JavaScript時鐘
我試圖做的是開始於一個時間值的時鐘(HH:MM:ss)表示我提供並以4倍速度運行(對於運行實際時間4倍的在線遊戲的服務器時間)。我修改了一個我在網上找到的免費時鐘來做到這一點,但它只適用於每隔一分鐘(嘗試下面的代碼,看看我的意思,如果沒有意義)。
var customClock = (function() {
var timeDiff;
var timeout;
function addZ(n) {
return (n < 10 ? '0' : '') + n;
}
function formatTime(d) {
t1 = d.getHours();
t2 = d.getMinutes();
t3 = d.getSeconds() * 4;
if (t3 > 59) {
t3 = t3 - 60;
t2 = t2 + 1;
}
if (t2 > 59) {
t2 = t2 - 60;
t1 = t1 + 1;
}
if (t1 > 23) {
t1 = 0;
}
return addZ(t1) + ':' + addZ(t2) + ':' + addZ(t3);
}
return function (s) {
var now = new Date();
var then;
var lag = 1015 - now.getMilliseconds();
if (s) {
s = s.split(':');
then = new Date(now);
then.setHours(+s[0], +s[1], +s[2], 0);
timeDiff = now - then;
}
now = new Date(now - timeDiff);
document.getElementById('clock').innerHTML = formatTime(now);
timeout = setTimeout(customClock, lag);
}
}());
window.onload = function() {
customClock('00:00:00');
};
任何想法爲什麼會發生這種情況?我對Javascript非常陌生,這絕對是一個小黑客。謝謝
我們不是有一個已經?目前找不到問題 – Bergi
我看了一下,找不到它,但是如果已經回答了鏈接,那就太好了。 – Chakron