定时请求数据

222 阅读1分钟

一、需求

image.png 每隔 5 分钟进行刷新请求得到最新的数据,关闭改信息后,切换到其他页面再切回来,也要让改信息显示出来,同时,切换到其他页面后不再做定时任务,再次回到该页面重新需要定时任务。

二、思路

结合 clearInterval 与 getCancelledNum 进行。

三、实现

methods() {
     getCancelledNum() {
        getCancelingNum().then((res) => {
          this.cancelledNum = res.data;
          this.isShow = true;
        });
  }
}


mounted() {
    this.getCancelledNum();
    this.timer = setInterval(() => {
      this.getCancelledNum();
    }, 300000);
}

destroy() {
    clearInterval(this.timer);
}

activated() {
    this.getCancelledNum();
    clearInterval(this.timer);
    this.timer = setInterval(() => {
      this.getCancelledNum();
    }, 300000);
}

deactivated() {
    clearInterval(this.timer);
}