该倒计时方法具有以下该功能:
1. 根据指定日期与当前的电脑时间进行匹配
2. 通过指定一个数组参数,来设置在每一天内不同的时间段进行倒计时。
function countDown(date,target,filter){
var setTime = new Date(date).getTime(),
var timer = null;
function core(){
var nowTime = new Date().getTime(),
var leftTime = 0,
var d = 0,h = 0,m = 0,s = 0;
if(filter.length){
setTime = new Date();
for(var i=0,l=filter.length;i<l;i++){
setTime.setHours(filter[i]);
setTime.setMinutes(0);
setTime.setSeconds(0);
if(nowTime < setTime.getTime()){
break;
}else if(i == filter.length-1){
setTime.setDate(setTime.getDate()+1);
setTime.setHours(filter[0]);
}
}
}
leftTime = Math.ceil((setTime – nowTime)/1000);
if(nowTime <= setTime){
d = ~~(leftTime/86400);
h = ~~(leftTime%86400/3600);
m = ~~(leftTime%86400%3600/60);
s = ~~(leftTime%86400%3600%60);
timer = setTimeout(core,1e3);
}else{
clearTimeout(timer);
timer = null;
}
//配置格式
target.innerHTML = ‘d:’+d+’ h:’+h+’ m:’+m+’ s:’+s;
}
core();
}
调用方式:
// 普通调用
countDown(‘2016/10/02/23:43’,oDiv);
// 指定时间循环倒计时
countDown(‘2016/10/02/23:43’,oDiv,[9,11,18]);
文档来源:http://www.cnblogs.com/HCJJ/p/5929720.html
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14988.html