倒计时函数

// 倒计时功能对象
var countDown = {
// 时间变量:天、时、分、秒
dayTime : '',
hoursTime : '',
minuteTime : '',
secondTime : '',
// 倒计时函数
count_down : function(callback){
var countDown = setTimeout(()=>{
this.secondTime--;
if(this.secondTime < 0 ){
if(this.minuteTime > 0){
this.secondTime = 59;
this.minuteTime --;
}else if(this.hoursTime > 0){
this.hoursTime--;
this.minuteTime = 59;
this.secondTime = 59;
}else if(this.dayTime > 0){
this.dayTime --;
this.hoursTime = 23;
this.minuteTime = 59;
this.secondTime = 59;
}else{
this.dayTime = 0 ;
this.hoursTime = 0;
this.minuteTime = 0;
this.secondTime = 0;

}
}
if(this.dayTime == 0 && this.minuteTime == 0 && this.hoursTime == 0 && this.secondTime == 0 ){
clearTimeout(countDown);
}else{
this.count_down(callback);
}
// 继续调用自身函数
callback(this.dayTime,this.hoursTime,this.minuteTime,this.secondTime)
},1000)
},
// 初始化函数
TimesTransfrom : function(Arr,callback){
var _y = Arr[0] || Arr[0] === 0 ? Arr[0] : new Date().getFullYear();
var _M = Arr[1] || Arr[1] === 0 ? Arr[1] : new Date().getMonth();
var _d = Arr[2] || Arr[2] === 0 ? Arr[2] : new Date().getDate();
var _h = Arr[3] || Arr[3] === 0 ? Arr[3] : new Date().getHours();
var _m = Arr[4] || Arr[4] === 0 ? Arr[4] : new Date().getMinutes();
var _s = Arr[5] || Arr[5] === 0 ? Arr[5] : new Date().getSeconds();
var _times = new Date(_y,_M,_d,_h,_m,_s) - new Date();
this.dayTime = Math.floor(_times/1000/60/60/24);
this.hoursTime = Math.floor(_times/1000/60/60%24);
this.minuteTime = Math.floor(_times/1000/60%60);
this.secondTime = Math.floor(_times/1000%60);
this.count_down(callback);
}
}
// 函数调用
countDown.TimesTransfrom([2018,4,8,0,0,0],function(d,h,m,s){
document.body.innerHTML = "<p>"+d+"天"+h+"时:"+m+"分:"+s+"秒</p>"
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值