最近做审核平台,涉及到多人分配审核数据,就需要定时自动刷新数据防止多人重复审核,其实就是页面加一个定时器,固定时间请求接口刷新数据,
先回想下vue的生命周期
在 mounted 周期时,页面结构已经生成,可以进行数据请求,所以我把定时器定义在了这里
mounted(){
if (this.timer) {
clearInterval(this.timer);
} else {
this.timer = setInterval(() => {
this.search(); // methods中请求数据的方法
}, 3000);
}
},
然后,当页面关闭的时候,应该把这个定时器清除,我选择在 destroyed 周期
destroyed() {
clearInterval(this.timer);
}