<input id="countdown" type="text" value="140时50分20秒">
<script> var oCountdown = document.getElementById("countdown"), nowStamps = Date.now() - 100, // 服务器当前时间 endStamps = Date.now() + 50000 - 100, // 活动结束时间戳 diff = nowStamps - Date.now(); // 本地时间和服务器时间差 function updateCountdown(nowStamps) { var timeLeft = (endStamps - nowStamps)/1000, hour = parseInt(timeLeft/3600), minute = parseInt(timeLeft%3600/60), seconds = parseInt(timeLeft%60); oCountdown.value = hour + "时" + minute + "分" + seconds + "秒"; return timeLeft; } function run() { nowStamps = Date.now() + diff; // 服务器当前时间 if (updateCountdown(nowStamps) > 0) { // 更新倒计时时间 setTimeout(run, 100); // fix + 100 调整误差 } else { oCountdown.value = "活动已结束"; } } setTimeout(run, 100); </script>
原文:http://www.cnblogs.com/qiangspecial/p/3915179.html