需求:在项目中需要每隔五秒请求一次接口

第一种方法:直接在mounted钩子函数中处理 

  mounted() {
    window.setInterval(() => {
      setTimeout(this.statusList(), 0);
    }, 5000);
  },

 

第二种方法:利用watch 去检测数据是否改变了

在data中定义一个变量 resultList:[]
在methos方法中 写一个方法
    timer() {
      return setTimeout(() => {
        this.statusList();   //这个就是我的接口
      }, 1000);
    },

在watch中检测
  watch: {
    resultList() {
      this.timer();
    }
  },

最后记得销毁

  destroyed() {
    clearTimeout(this.timer);
  },

 

相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2021-04-20
  • 2022-12-23
  • 2021-04-17
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2021-11-10
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案