-
Notifications
You must be signed in to change notification settings - Fork 17
Home
王员外 edited this page May 21, 2014
·
1 revision
var defined = { nowTime: 0 , endTime : 0 , update : function( ){} , finish : function(){} } , _opts = $.extend({}, defined, opt ) , offsetTime = 0 //增长 , timeOut = setInterval(function(){ var info = getTime(); _opts.update( info.day, info.hour, info.minute, info.second ); if( info.day == 0 && info.hour == 0 && info.minute == 0 && info.second == 0 ){ //完成 _opts.finish( _opts); clearInterval( timeOut ); } offsetTime += 1e3; }, 1000) ;
function getTime(){
var cs, cm, ch, cd
, offset = parseFloat( offsetTime ) || 0
, cris = new Date( _opts.endTime ).getTime()//这里定义的是需要倒计时的时间,并转换成时间戳
, now = new Date( _opts.nowTime ).getTime()//获取当前时间,并转换成时间戳
, cTotal = cris - now - offset
, as = 1000//时间戳以毫秒为单位,所以1秒为1000毫秒,以下分别定义1分钟,1小时和1天
, am = as * 60
, ah = am * 60
, ad = ah * 24
;
//获取倒计时的天数,小时数,分钟数和秒数
cd = Math.floor(cTotal / ad);
ch = Math.floor((cTotal - cd * ad) / ah);
cm = Math.floor((cTotal - cd * ad - ch * ah) / am);
cs = Math.floor((cTotal - cd * ad - ch * ah - cm * am) / as);
// cd + "天" + ch + "小时" + cm + "分" + cs + "秒";
var out = {
day : cd
, hour : ch
, minute : cm
, second : cs
};
return out;
}