封装的倒计时方法

function downTime (time) {//time是结束时间
var timesNow = Date. parse ( new Date()); //当前时间
var leftTime = time - timesNow ; //剩余时间
var leftsecond = parseInt ( leftTime / 1000 ); //剩余秒数
var day_1 = Math . floor ( leftsecond / ( 60 * 60 * 24 )); //剩余天数
var hour = Math . floor (( leftsecond - day_1 * 24 * 60 * 60 ) / 3600 ); //剩余小时
var minute = Math . floor (( leftsecond - day_1 * 24 * 60 * 60 - hour * 60 * 60 ) / 60 ); //剩余分钟
var second = Math . floor ( leftsecond - day_1 * 24 * 3600 - hour * 3600 - minute * 60 ); //剩余秒数
hour = checkTime ( hour );
minute = checkTime ( minute );
second = checkTime ( second );
str = "" + day_1 + "" + hour + "" + minute + "" + second + "秒" ;
return str ;
function checkTime (i) { //将0-9的数字前面加上0,例1变为01
if (i < 10 ) {
i = "0" + i;
}
return i;
}
}

最后定时器执行函数:

setInterval(downTime(time),1000);

你可能感兴趣的:(个人小总结)