簡體   English   中英

JavaScript 塊循環邏輯

[英]JavaScript block loop logic

我的代碼有一個包含許多對象的數組,這些對象是 URL 的,我用它們創建一個循環來遍歷這個數組,並對數組中的每個 url 發出請求。 但是我需要通過塊數組循環到 go,例如:每 10 個; 直到你到達終點,所有這一切都是為了你不會立即這樣做並最終使服務器超載並且也不會花太長時間從 1 到 go 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);

或者類似的東西,希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM