vue 定时器 定时刷新页面 定时请求接口
data(){
return{
intervalId:null,
}
},
methods:{
// 定时刷新数据函数
dataRefreh() {
// 计时器正在进行中,退出函数
if (this.intervalId != null) {
return;
}
// 计时器为空,操作
this.intervalId = setInterval(() => {
console.log("刷新" + new Date());
this.getHeaderData(); //加载数据函数
}, 500);
},
// 停止定时器
clear() {
clearInterval(this.intervalId); //清除计时器
this.intervalId = null; //设置为null
},
getHeaderData(){
//请求接口
}
},
created() {
this.dataRefreh();
},
destroyed(){
// 在页面销毁后,清除计时器
this.clear();
}
浙公网安备 33010602011771号