我的代码有一个包含许多对象的数组,这些对象是url,我用它们做了一个循环,循环遍历这个数组,并对数组中的每个url发出请求。但我需要我的循环以块的形式遍历数组,例如:每10个;,直到你到达最后,所有这些都是为了你不会立即这么做,最终导致服务器过载,也不会花太长时间从1进1,因为数组非常大,代码库基本上就是这样。
const array = ["URL-1","URL-2","URL-3"] etc...
for (var i = 0; i < array.length; i++) {
axios.get(array[i], (res) => {
const res = res;
});
}发布于 2020-10-11 01:56:29
有一种更好的方法,但通过使用setTimeOut选项,您可以尝试
let index = 0;
const maxIterations = 10;
function checkUrls() {
for (var i = index; i < index + maxIterations; i++) {
axios.get(array[i], (res) => {
const res = res;
}
index += maxIterations;
if (index >= array.length - maxIterations) {
endOfArray = true;
clearInterval(timer);
}
}
const timer = setInterval(checkUrls, 10000);或者其他类似的东西,希望能有所帮助
https://stackoverflow.com/questions/64296261
复制相似问题