JavaScript 块循环逻辑
JavaScript block loop logic
我的代码有一个包含许多对象的数组,这些对象是 URL'S,我用它们创建一个循环遍历这个数组并为每个 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;
});
}
大多数情况下都有更好的方法,但您可以尝试使用 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);
或类似的东西,希望对您有所帮助
我的代码有一个包含许多对象的数组,这些对象是 URL'S,我用它们创建一个循环遍历这个数组并为每个 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;
});
}
大多数情况下都有更好的方法,但您可以尝试使用 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);
或类似的东西,希望对您有所帮助