![](http://static.javashuo.com/static/loading.gif)
倒計時
(function() {
var $actime, $actitle, $endtime, $nowtime, $starttime, acinfo, init, timer;
$actime = document.getElementById('ac-time');
$actitle = document.getElementById('ac-title');
$endtime = new Date("2013/09/27 00:00:00");//系統結束時間
$nowtime = new Date("2013/09/25 15:05:40").getTime();//系統當前時間
$starttime = new Date("2013/09/23 17:43:11"); //系統結束時間
acinfo = {};
/*活動狀態信息 0表示未開始,1表示已開始,2表示已結束*/
acinfo.status = 0;
/*切換提示標題函數*/
acinfo.changeTitle = function(status) {
var $title;
$title = ['距開始時間', '剩餘時間', '活動已結束'];
return $actitle.innerHTML = $title[status];
};
acinfo.changetime = function(time, status) {
return $actime.innerHTML = status !== 2 ? '<i class="cf90">' + time.d + '</i>天<i class="cf90">' + time.h + '</i>時<i class="cf90">' + time.m + '</i>分' : '活動已結束';
};
acinfo.changeStatus = function() {
if ($starttime.getTime() > $nowtime) {
return this.status = 0;
} else if ($nowtime > $endtime.getTime()) {
return this.status = 2;
} else {
return this.status = 1;
}
};
acinfo.update = function(status, refreshTime/*setInterval時間*/) {
var cTime, dhm, i, time;
time = {};
i = 1;
$nowtime = $nowtime + (i++) * refreshTime;
cTime = status === 0 ? $starttime : $endtime;
dhm = parseInt((cTime.getTime() - $nowtime) / 1000);
time.d = parseInt(dhm / 3600 / 24);
time.h = parseInt((dhm / 3600) % 24);
time.m = parseInt((dhm / 60) % 60);
acinfo.changeTitle(status);
return acinfo.changetime(time, status);
};
init = function() {
acinfo.changeStatus();
if (acinfo.status === 2) {
clearInterval(timer);
}
return acinfo.update(acinfo.status, 60000);
};
init();
timer = setInterval(init, 60000);
}).call(this);