How to gracefully clear the timer in vue

$once listens for a custom event, but only fires it once. Once triggered, the listener is removed.

程序化清理监听器,在组件内的beforeDestory钩子中清除

  mounted () {
    
    
    const that = this
    // 设置定时器
    const timer= setInterval(() => {
    
    
      setTimeout(() => {
    
    
        console.log('test clearInterval')
      }, 0)
    }, 2000)
    // 通过$once来监听生命周期beforeDestroy钩子,在组件销毁前清除定时器。
    this.$once('hook:beforeDestroy', () => {
    
    
      clearInterval(timer)
    })

Guess you like

Origin blog.csdn.net/qq_42931285/article/details/124288085