起因:写一个项目的倒计时使用了setInterval,但是ios下会引起内存泄漏,页面卡死的问题。所以这里记录下正确的处理方式。

export default {
  data() {
    return {
      timer: '',
      value: 0
    }
  },
  methods: {
    get() {
      this.value ++
      console.log(this.value)
    }
  },
  mounted() {
    this.timer = setInterval(this.get, 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}

export default {
  data() {
    return {
      timer: '',
      value: 0
    }
  },
  methods: {
    get() {
      this.value ++
      console.log(this.value)
    }
  },
  mounted() {
    this.timer = setTimeout(this.get, 1000)
  },
  beforeDestroy() {
    clearTimeout(this.timer)
  }
}

# To Be Continued!😎

Last Updated: 11/17/2021, 9:33:34 PM