vue2中从页面1跳转到页面2后,页面2还会请求页面1中的timeout,解决办法

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);
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值