活动倒计时脚本,根据当前活动状态来修改活动提示及活动的时间
倒计时 (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);
如果有任何扩展,只需要去增加当前活动的状态就可