ios返回当前页定时器没有清除的问题

200 阅读1分钟

ios 由下一个页面返回当前页时,当前页的定时器没有清除

使用 pageshow 方法获取事件的 e.isTrusted ,当它为 true 时就表示重新返回了当前页,此时可执行清除定时器的操作

created () {
  window.addEventListener('pageshow', (e) => this.handleClearTimeout(e))
},
beforeDestroy() {
  window.removeEventListener('pageshow', (e) => this.handleClearTimeout(e))
},
methods: {
  handleClearTimeout(e) {
    if (e.isTrusted) { // 重新显示 即按了返回按钮
      this.timer1 && clearTimeout(this.timer1)
      this.timer2 && clearTimeout(this.timer2)
      this.timer3 && clearTimeout(this.timer3)
      this.loading = false
    }
  }
}