关于javascript:Day-4100-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;
  },

亲测,好使~

以上

参考链接

官网文档链接

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理