1、在data()中先定义定时器的名称
data() {
return {
timerForVehicleDetail: null, //请求小车详情定时器
timeout1: null,
timeout2: null,
timeout3: null,
timeout4: null,
timeout5: null,
timeout6: null,
timeout7: null,
};
2、在mounted中写定时器的方法(在mothods中也可以定义定时器)
mounted() {
this.timerForVehicleDetail = setInterval(() => {
this.getAllDataAsync();
this.getVehicleDetail();
this.checkTab(this.tabIndex);
}, 20 * 1000);
},
3、在beforeDestroy()中清楚页面1定义的定时器,这样跳转到其他页面就不会持续调用该页面的timeout
beforeDestroy() {
// 清除定时器
clearTimeout(this.timerForVehicleList);
clearTimeout(this.timerForVehicleDetail);
clearTimeout(this.timeout1);
clearTimeout(this.timeout2);
clearTimeout(this.timeout3);
clearTimeout(this.timeout4);
clearTimeout(this.timeout5);
clearTimeout(this.timeout6);
clearTimeout(this.timeout7);
},
4、
methods: {
checkTab(index) {
console.log("执行吗执行吗执行吗11111");
this.tabIndex = index;
if (!this.activeVehicle) {
// 如果没有选中的小车 不请求数据
return;
}
var data = "";
if (this.tabIndex == 0) {
data = this.transportTaskList;
this.timeout1 = setTimeout(() => {
this.$refs.transportTask.show(data);
}, 500);
} else if (this.tabIndex == 1) {
data = this.chargeTaskList;
this.timeout2 = setTimeout(() => {
this.$refs.rechargeTask.show(data);
}, 500);
} else if (this.tabIndex == 2) {
data = this.parkTaskList;
this.timeout3 = setTimeout(() => {
this.$refs.parkingTask.show(data);
}, 500);
} else if (this.tabIndex == 3) {
data = this.noTaskParkList;
this.timeout4 = setTimeout(() => {
this.$refs.noTaskParking.show(data);
}, 500);
} else if (this.tabIndex == 4) {
data = this.noTaskChargeList;
this.timeout5 = setTimeout(() => {
this.$refs.noTaskRecharge.show(data);
}, 500);
}
},
getVehicleDetail() {
if (!this.activeVehicle) {
return;
}
var data = {
// allRunTime: this.allRunTime,
usageRate: this.usageRate,
transportTime: this.transportTime,
parkTime: this.parkTime,
chargeTime: this.chargeTime,
differentTaskDurationsList: this.differentTaskDurationsList,
};
this.timeout6 = setTimeout(() => {
this.$refs.leftDateList.show(data);
}, 500);
this.timeout7 = setTimeout(() => {
this.$refs.rightStatusPie.show(data);
}, 500);
}
286

被折叠的 条评论
为什么被折叠?



