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

有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();
	},

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐