1.公用的方法:
// 倒计时
/*
参数:
hourTime:还剩多少毫秒;
hourDom:需要展示小时数的dom;
minuDom:需要展示分钟的dom;
secDom:需要展示秒数的dom;
callback:回调函数;
*/
function returnTimer(hourTime,hourDom, minuDom, secDom, callback) {
var maxtime = hourTime / 1000; //按秒计算,自己调整!
CountDown();
function CountDown() {
if (maxtime >= 0) {
if(hourDom){//有的倒计时可以不展示小时数,就把hourDom传值false;
var hours = Math.floor(Math.floor(maxtime / 60) / 60);
if (hours < 10) {
hourDom.text(0 + '' + hours);
} else {
hourDom.text(hours);
};
}
var minutes = Math.floor(Math.floor(maxtime / 60) % 60);
var seconds = Math.floor(maxtime % 60);
if (minutes < 10) {
minuDom.text(0 + '' + minutes);
} else {
minuDom.text(minutes);
};
if (seconds < 10) {
secDom.text(0 + '' + seconds);
} else {
secDom.text(seconds);
};
// if (maxtime == 5 * 60) alert("还剩5分钟");
--maxtime;
setTimeout(CountDown, 1000);
} else {
callback();
if(hourDom){
hourDom.text("00");
}
minuDom.text("00");
secDom.text("00");
}
};
};
2.计算剩余时间
//倒计时
function animateTime(){
var endDay =new Date(); //endDay 结束时间
endDay.setDate(new Date().getDate() + 1) ;
endDay.setHours(0);
endDay.setMinutes(0);
endDay.setSeconds(0);
var startTime = new Date(); //开始时间是现在
var remainTime = endDay - startTime;
returnTimer(remainTime, $(".ld-fb-time.minutes"), $(".ld-fb-time.seconds"), function(){},$(".ld-fb-time.hours"))
}
animateTime()