Day 4/100 Vue3 如何设置定时器以及清除定时器?
文章目录
前言
搜了一堆,是支持Vue2 的this.$once,试了有报错,发现Vue3 被移除了,伤心,记录下解决方案。
1、data 中声明
data () {
return {
timer: null,
}
},2、mounted 钩子中定义方案及处理函数
mounted () {
this.timer = setInterval(()=>{
//处理部分
},3000)
},3、beforeUnmount或unmounted的钩子中销毁
beforeUnmount() {
clearInterval(this.timer)
this.timer = null;
},亲测,好使~
以上

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论