export class BuildCountdown {
constructor (ms, timeEndCallback) {
this.timeEndCallback = timeEndCallback
this.setTimer(ms)
}
timer = null
remainSecond = 0
get hour () {
return this.filterString(Math.floor(this.remainSecond / 3600))
}
get minute () {
return this.filterString(Math.floor(this.remainSecond / 60 % 60))
}
get seconds () {
return this.filterString(Math.floor(this.remainSecond % 60))
}
get outPutString () {
return `${this.hour}时${this.minute}分${this.seconds}秒`
}
setTimer (ms) {
if (!ms || ms < 0) return
this.remainSecond = ms / 1000
this.timer = setInterval(() => {
if (this.remainSecond <= 0) {
this.handleTimeEnd()
return
}
this.remainSecond--
}, 1000)
}
handleTimeEnd () {
this.removeTimer()
this.timeEndCallback && this.timeEndCallback()
}
removeTimer () {
clearInterval(this.timer)
this.timer = null
}
filterString (value) {
if (value < 0) return 0
else return value >= 10 ? value : `0${value}`
}
}
console