清除延时器、定时器

106 阅读1分钟

除了在beforeDestroy()生命周期里清除以外,还可以通过$once这个事件侦听器在定义完定时器之后的位置来清除定时器/延时器

mounted(){ 
    const timer = setInterval(()=>{ 
        console.log(1) 
    },1000) 
    this.$once('hook:beforeDestroy',()=>{ 
        clearInterval(timer) 
    }) }