本文以一個小實例帶入 Date 物件的基本操作,適合初學者入門(熟悉的朋友可以直接跳過)。需求有兩個:一是時間每秒自動更新且不刷新頁面;二是點擊時間可以暫停,再點一次繼續走。
既然是時間,自然要用 new Date()。與其一個個變數宣告,不如收攏到一個物件裡,取用時寫 time.year 就好,結構清楚也好維護。
var nowDate = new Date();
var time = {
year: nowDate.getFullYear(),
month: nowDate.getMonth(),
day: nowDate.getDate(),
week: nowDate.getDay(),
hour: nowDate.getHours(),
minute: nowDate.getMinutes(),
second: nowDate.getSeconds()
};
getDay() 回傳的是數字,得轉成人看得懂的「星期一」。這裡用 switch 判斷最合適(當然用 if / else 也行,看個人習慣)。
function Week(num) {
switch (num) {
case 1: return '星期一';
case 2: return '星期二';
case 3: return '星期三';
case 4: return '星期四';
case 5: return '星期五';
case 6: return '星期六';
case 7: return '星期日';
}
}
另一個小問題:分與秒在 0~9 之間時會顯示成「5」而不是「05」,看起來就是不太對。寫個小函式補上前面的 0:
function twoNum(num) {
return num = num < 10 ? '0' + num : num;
}
等價的 if 寫法:
function twoNum(num) {
if (num < 10) {
num = '0' + num;
}
return num;
}
function Timer(obj) {
var nowDate = new Date();
var time = {
year: nowDate.getFullYear(),
month: nowDate.getMonth(),
day: nowDate.getDate(),
week: nowDate.getDay(),
hour: nowDate.getHours(),
minute: nowDate.getMinutes(),
second: nowDate.getSeconds()
};
function Week(num) {
switch (num) {
case 1: return '星期一';
case 2: return '星期二';
case 3: return '星期三';
case 4: return '星期四';
case 5: return '星期五';
case 6: return '星期六';
case 7: return '星期日';
}
}
function twoNum(num) {
return num = num < 10 ? '0' + num : num;
}
obj.innerHTML = time.year + '年' + time.month + '月' + time.day + '日 ' +
Week(time.week) + ' ' +
time.hour + ':' + twoNum(time.minute) + ':' + twoNum(time.second);
}
傳入一個 obj,時間就會輸出到該元素裡。不過此時它還只是一個靜態畫面,頁面不動它就不會跳,所以接著要加上定時器。
先在頁面上準備一個容器:
<div id="box"></div>
自動更新有兩種定時器可選:setInterval() 會持續執行直到被清除;setTimeout() 只跑一次,想反覆執行得自己遞迴呼叫。這裡選前者:
var oBox = document.getElementById("box");
Timer(oBox); // 先執行一次,否則會有約 1 秒的延遲,看起來像慢了一拍
oBox.timer = setInterval(function () {
// 把 timer 掛在元素上,可以避免污染全域變數,也能避開命名衝突
Timer(oBox);
}, 1000);
這其實就是一個「開關」的問題:再按一次亮,再按一次滅。設一個布林旗標即可:
var offOn = true;
oBox.onclick = function () {
if (offOn) {
clearInterval(oBox.timer);
offOn = false;
} else {
oBox.timer = setInterval(function () {
Timer(oBox);
}, 1000);
offOn = true;
}
};
var oBox = document.getElementById("box");
var offOn = true;
Timer(oBox);
function showTime() {
oBox.timer = setInterval(function () {
Timer(oBox);
}, 1000);
}
showTime();
oBox.onclick = function () {
offOn ? clearInterval(oBox.timer) : showTime();
offOn = !offOn;
};
function Timer(obj) {
var nowDate = new Date();
var time = {
year: nowDate.getFullYear(),
month: nowDate.getMonth(),
day: nowDate.getDate(),
week: nowDate.getDay(),
hour: nowDate.getHours(),
minute: nowDate.getMinutes(),
second: nowDate.getSeconds()
};
function Week(num) {
switch (num) {
case 1: return '星期一';
case 2: return '星期二';
case 3: return '星期三';
case 4: return '星期四';
case 5: return '星期五';
case 6: return '星期六';
case 7: return '星期日';
}
}
function twoNum(num) {
return num = num < 10 ? '0' + num : num;
}
obj.innerHTML = time.year + '年' + time.month + '月' + time.day + '日 ' +
Week(time.week) + ' ' +
time.hour + ':' + twoNum(time.minute) + ':' + twoNum(time.second);
}
這裡用到的幾處三元運算與取反(offOn = !offOn)值得細細體會一下,寫法雖然精簡,可讀性也不差。
顯示時間其實只是 Date 物件應用的九牛一毛,實務上更常見的是倒計時——團購頁的「剩餘時間」、註冊時的「驗證碼 X 秒後重發」、活動倒數等等。核心概念一樣:算出目標時間與當前時間的毫秒差,再換算成天、時、分、秒,最後同樣用定時器每秒刷新一次。這個主題值得另外寫一篇來細談。
以上就是「在頁面中輸出當前客戶端時間」的完整實作,希望對大家有所幫助!