電商類網站的拍賣活動常見倒數計時,年會與抽獎活動則常見名單快速滾動後停格的設計。這兩種效果的原理都不複雜,以下分別實作。
核心邏輯是「截止時間減當前時間」,再把毫秒差換算成天、時、分、秒。範例中用了一個變數 i 做微小補償,避免多次讀取系統時間造成的累積誤差。
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒計時效果的實現</title>
</head>
<body>
<form runat="server">
<div id="timer">計算中。。。</div>
<script type="text/javascript">
var i = 0;
function TimeTo(dd, nowtstr) {
var t = new Date(dd), // 取得指定時間
n = (new Date(nowtstr)) - (-100 * i), // 取得當前時間
c = t - n; // 時間差
if (c <= 0) { // 已過期或剛好到期
document.getElementById('timer').innerHTML = '活動已經結束';
clearInterval(window['ttt']);
return;
}
var ds = 60 * 60 * 24 * 1000, // 一天的毫秒數
d = parseInt(c / ds), // 相差天數
h = parseInt((c - d * ds) / (3600 * 1000)), // 小時
m = parseInt((c - d * ds - h * 3600 * 1000) / (60 * 1000)), // 分鐘
s = parseInt((c - d * ds - h * 3600 * 1000 - m * 60 * 1000) / 1000); // 秒
document.getElementById('timer').innerHTML =
'<p style="margin-top:5px;"> <b>' + d + '</b> 天 <b>' + h + '</b> 小時 <b>'
+ m + '</b> 分<b>' + s + '</b> 秒</p>';
i++;
}
(function () {
window['ttt'] = setInterval(function () {
var timestr = "2013-10-21"; // 也可由後台傳入活動截止時間,格式 "yyyy-MM-dd"
var nowtstr = "2013-08-23"; // 當前時間,格式同上
if (timestr != "") {
TimeTo(timestr, nowtstr);
}
}, 100); // 每 100 毫秒計算並更新一次
})();
</script>
</form>
</body>
</html>
若想把天、時、分、秒分別放進不同欄位(例如做成表格),只需分別更新各元素:
<table border="1">
<tr>
<th id="day" width="100"></th>
<th id="day2" width="100"></th>
<th id="day3" width="100"></th>
<th id="day4" width="100"></th>
</tr>
</table>
<script type="text/javascript">
function timestr()
{
var c = Date.parse("2014-11-11") - Date.parse(new Date());
if (c <= 0)
{
alert('活動已經結束');
clearInterval(aa);
}
var ds = 60 * 60 * 24 * 1000,
d = parseInt(c / ds),
h = parseInt((c - d * ds) / (3600 * 1000)),
m = parseInt((c - d * ds - h * 3600 * 1000) / (60 * 1000)),
s = parseInt((c - d * ds - h * 3600 * 1000 - m * 60 * 1000) / 1000);
document.getElementById('day').innerHTML = '<b>' + d + '</b>天';
document.getElementById('day2').innerHTML = '<b>' + h + '</b> 時';
document.getElementById('day3').innerHTML = '<b>' + m + '</b> 分';
document.getElementById('day4').innerHTML = '<b>' + s + '</b> 秒';
}
var aa = setInterval(timestr, 1000);
</script>
實際運行結果如下:


思路是:開始抽獎時啟動一個短間隔的計時器,讓文字框高速輪換名字;按下「獲取幸運觀眾」時清掉計時器,此時停在/name上的就是結果。
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文字滾動效果的實現</title>
<script type="text/javascript">
function TimeTo(dd) {
document.getElementById('TextBox1').value = dd;
}
$(function () {
window['ttt'] = setInterval(aaa, 100); // 頁面載入時即開始滾動
});
function stopInterval()
{
clearInterval(window['ttt']);
window['ttt'] = "";
}
function aaa() {
var aa = "張三,李四,王五,趙六,孫七,胡八,曾九,劉十,伊一,樸二";
var arr = aa.split(',');
var rdd = 9 * Math.random();
var leth = Math.round(rdd);
TimeTo(arr[leth].toString());
}
function TimeTo2(){
if (window['ttt'] == "") {
window['ttt'] = setInterval(aaa, 100);
}
}
</script>
</head>
<body>
<form runat="server">
<input type="text" id="TextBox1"/>
<input type="button" value="開始抽獎" onClick="TimeTo2();"/>
<input type="button" value="獲取幸運觀眾" onClick="stopInterval();"/>
</form>
</body>
</html>
效果如下:


var myDate = new Date(); myDate.getYear(); // 年份(2 位) myDate.getFullYear(); // 完整年份(4 位) myDate.getMonth(); // 月份(0-11,0 代表 1 月) myDate.getDate(); // 日(1-31) myDate.getDay(); // 星期(0-6,0 代表週日) myDate.getTime(); // 從 1970.1.1 起算的毫秒數 myDate.getHours(); // 小時(0-23) myDate.getMinutes(); // 分鐘(0-59) myDate.getSeconds(); // 秒(0-59) myDate.getMilliseconds(); // 毫秒(0-999) myDate.toLocaleDateString(); // 日期 myDate.toLocaleTimeString(); // 時間 myDate.toLocaleString(); // 日期與時間
· 倒計時的目標時間應以伺服器時間為基準,只用前端時間會被使用者改時鐘繞過
· 抽獎若用 Math.random,务必在伺服器端執行真正抽取,前端滾動純粹是視覺演出
· 計時器記得在活動結束或元件卸載時 clearInterval,否則容易累積游離計時器
· 100 毫秒的更新頻率對倒計時而言偏高,改成 1000 毫秒並對齊秒邊界會更省資源