倒计时工具函数,页面里直接引入使用setInterval一调用即可
function timeDownFn (et){
let countdown = {
day:'',
hour:'',
miun:'',
swc:''
}
const startTime = +new Date()
const endTime = +new Date(et)
// 倒计时总秒数
const timesall = (endTime-startTime)/1000;
// 天
countdown.day = Math.floor(timesall / 60 / 60 / 24) >=10? Math.floor(timesall / 60 / 60 / 24):'0'+Math.floor(timesall / 60 / 60 / 24);
// 时
countdown.hour = Math.floor((timesall / 60 / 60) % 24)>=10? Math.floor((timesall / 60 / 60) % 24):'0'+Math.floor((timesall / 60 / 60) % 24) ;
// 分
countdown.miun = Math.floor(( timesall / 60) % 60)>=10?Math.floor(( timesall / 60) % 60):'0'+Math.floor(( timesall / 60) % 60) ;
// 秒
countdown.swc = Math.floor(timesall % 60 )>=10?Math.floor(timesall % 60 ):'0'+Math.floor(timesall % 60 ) ;
return countdown;
}
export default timeDownFn;