嗯,我試圖重新在這裏看到每個這些挑戰的倒計時: http://www.bungie.net/stats/reach/globalchallenges.aspx問題的JavaScript倒計時
的問題是,如果你比較my countdown與original countdown有大約10-14秒的差別,我只是不明白爲什麼?謝謝!
我正在使用其來源mobile page(查看源代碼)來獲取到期時間。
<span id="wExpSeconds" title="1304330400000"></span>
<span id="dExpMilliseconds" title="1303984800000"></span>
這是我的倒數網頁的源代碼:
<html>
<head>
<title></title>
<style type="text/css">
.stat{ font: 14px/18px Arial, Helvetica, sans-serif; color:#bbb; }
.seconds{ color:#ff5f3c; }
</style>
</head>
<body>
<div id="countDownDiv"></div>
<script type="text/javascript">
function countDown(id, end, cur){
this.container = document.getElementById(id);
this.endDate = new Date(end);
this.curDate = new Date(cur);
var context = this;
var formatResults = function(day, hour, minute, second){
var displayString = [
'<span class="stat">',day,'d </span>',
'<span class="stat">',hour,'h </span>',
'<span class="stat">',minute,'m </span>',
'<span class="stat seconds">',second,'s</span>'
];
return displayString.join("");
}
var update = function(){
context.curDate.setSeconds(context.curDate.getSeconds()+1);
var timediff = (context.endDate-context.curDate)/1000;
// Check if timer expired:
if (timediff<0){
return context.container.innerHTML = formatResults(0,0,0,0);
}
var oneMinute=60; //minute unit in seconds
var oneHour=60*60; //hour unit in seconds
var oneDay=60*60*24; //day unit in seconds
var dayfield=Math.floor(timediff/oneDay);
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour);
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute);
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute));
context.container.innerHTML = formatResults(dayfield, hourfield, minutefield, secondfield);
// Call recursively
setTimeout(update, 1000);
};
// Call the recursive loop
update();
}
countDown("countDownDiv",1304330400000, new Date().getTime());
</script>
</body>
</html>
我只顯示一個6秒的差異 – 2011-04-28 01:31:08
由於它們在一個線程中執行的事實,JavaScript定時器是不可靠的:http://ejohn.org/blog/how-javascript-timers-work/。不知道這是否是您的問題,但很高興知道您是否期望計時器正確執行*。 – 2011-04-28 01:40:21
很奇怪,我看到在時間上的精確匹配 – 2011-04-28 01:53:42