前端

vue定时器的使用(文字待整理)

全文参考 https://www.cnblogs.com/yunjiao/p/16594098.html 这里只是总结本次查找问题的解决重点: $on,$once本次都没用到,主要是计时器循环打开关闭需要先判断一下,是否为null,否则会指数级增长 let


这里只是总结本次查找问题的解决重点:
$on,$once本次都没用到,主要是计时器循环打开关闭需要先判断一下,是否为null,否则会指数级增长
在子组件的function edit(record) {里面初始化timer
然后在父组件的handleCancel内清空计时器,

再次通过父组件,打开子组件时,需要判空,才能初始化计时器。

if (timer.value != null) {
      return;
    }

全文参考 https://www.cnblogs.com/yunjiao/p/16594098.html
let timer = setInterval(()=>{
      console.log("setInterval");
    },1000)
    //  一次性侦听一个事件,只执行一次
    this.$once("hook:beforeDestroy", ()=>{
      clearInterval(timer);
      timer = null;
    })

 

this.$on("hook:activated", () => {
      if (timer === null) { // 避免重复开启定时器
        timer = setInterval(()=>{
          console.log("setInterval");
        },1000)
      }
    })