H5用js倒計(jì)時(shí) 手機(jī)切換到后臺(tái)倒計(jì)時(shí)暫停問題處理

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)
      }
},
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容