如果大屏需要请求很多接口,那么采用这个方法可以减缓一次请求数十个接口的压力。第一次全部请求完,第二次开始慢慢循环,一个一个请求。
有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();
},