vue数据大屏间隔请求发方法 && 定时发送请求以及销毁

如果大屏需要请求很多接口,那么采用这个方法可以减缓一次请求数十个接口的压力。第一次全部请求完,第二次开始慢慢循环,一个一个请求。

有10个接口,五秒钟刷新一次,每个接口请求次数间隔0.5旧可以了,非常的方便

//间隔请求方法
   beforeDestroy() {
    clearInterval(this.intervalId); // 在组件销毁前清除间隔事件
  },

  methods: {
    getInterval() {
      const requests = [
        this.getInAndOutWarehouseTomonth.bind(this),
        this.getSashHumTem.bind(this),
        this.selectQuestionnaire.bind(this),
        this.getAnnualStatistics.bind(this),
        this.selectTotalInventory.bind(this)
      ];
      let index = 0;
      const interval = 1000; // 间隔时间
      const executeRequest = () => {
        if (index >= requests.length) {
          index = 0; // 重新开始从第一个请求执行
        }
        requests[index](); // 执行当前请求
        index++; // 索引值加一
      };
      this.init(); // 立即执行一次init函数
      this.intervalId = setInterval(executeRequest, interval);
    },
}

普通定时发送请求

//初始化
	onLoad(options) {
		this.startTimer()
     },

methods: {
clearTimer() {
			// 清除定时器
			clearInterval(this.timer);
		},

startTimer() {
			// 设置定时器,每10秒钟触发一次getData方法
			this.timer = setInterval(() => {
				this.getdetail();
			}, 3000);
		},
getdetail(){
//在这里请求
}
},

	destroyed() {
		// 组件销毁时清除定时器
		this.clearTimer();
	},

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值