H5在息屏或者切換到后臺(tái),在打開的時(shí)候,短信倒計(jì)時(shí)會(huì)從原來(lái)的值開始,修復(fù)這個(gè)問題需要用到 visibility屬性
我用的是vue
window.addEventListener('popstate', this.goBack)
document.addEventListener('visibilitychange', this.resetTime)
}
beforeDestroy() {
window.removeEventListener('popstate', this.goBack)
window.removeEventListener('visibilitychange', this.resetTime)
}
resetTime() {
if (document.hidden) {
this.closeTIme = Date.now()
} else {
this.countDown()
}
countDown() {
this.showCount = true
if (!this.timer) {
this.count = TIME_COUNT
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--
} else {
this.closeTime = ''
clearInterval(this.timer)
this.timer = null
}
}, 1000)
} else if (this.closeTime) {
this.count =
this.count - ((Date.now() - this.closeTime) / 1000).toFixed(0)
}
},