最近项目有个接口要遍历去请求数据(别问,问就是后端只给单个的数据),同时发出几十个请求,而且接口又慢!!只能做操作时取消请求处理。
cancelAll(){
for(let i = 0;i < window.pending.length;i++){
const item = window.pending[i]
item.c()
}
}
getIndexReport(){
window.pending = []
this.system.nodes.forEach(item => {
let params = {subSysCode : item.id, type:1}
const CancelToken = axios.CancelToken;
let cancel;
let timer = setTimeout(()=>{
cancel()
this.$message.error("请求超时)
},10000)
this.axios({
methods:"get",
url:"",
params:params,
cancelToken:new CancelToken(function executor(c){
cancel=c
window.cancel=c
window.pending.push({c})
})
}).then(res=>{
clearTimeout(timer)
...
}).catch(()=>{
clearTimeout(timer)
})
})
}