方法1(推荐)
mounted: function () {
let listInfo = setInterval(() => {
setTimeout(() => {
this.getDataList();
}, 0);
}, 5 * 60 * 1000);
let alarmInfo = setInterval(() => {
setTimeout(() => {
// 获取告警信息
that.getAlarmInfo();
}, 0);
}, 5 * 1000);
this.$once("hook:beforeDestroy", () => {
clearInterval(listInfo);
clearInterval(alarmInfo);
listInfo = null;
alarmInfo = null;
});
},
方法2
首先在data定义定时器变量名,最后在beforeDestroy()生命周期内清除定时器:
data() {
return {
timer: null // 定时器名称
}
},
mounted() {
this.timer = setInterval(() => {
// 定时器操作
}, 1000)
},
// 销毁组件前清除定时器
beforeDestroy() {
clearInterval(this.timer);
this.timer = null;
}