Two different timeouts in same loop

2273 views javascript
9

I have a web crawler that is working fine, but i have an issue. What I basically need is when I reach 90 requests I need to give a timeout of 5 or more minutes.

my example code:

for (let i=0; i < maxPag; i++) {
    setTimeout(function(){
        console.log(i);
        getInfoPage(i);

    }, 15000 * (i+1));
} 

For example in the example above i give a timeout but is for the page that is requested, but after 90 requests, i need to have like a bigger timeout, this happens in all times that i reach in 90 requests.

How can i make a interval in the 90th request, than in 180th request, and goes on untill my for loop ends.

answered question

So if I understood correctly, you want your timeout after every 90 request to be 5 minutes?

2 Answers

4

for (let i=0; i < maxPag; i++) {
if( i === 89){                  // loop  90th
 setTimeout(function(){
        console.log(i);
        getInfoPage(i);

    }, 15000 * (i+1));
}else{
/*some codes here *?
}

} 

posted this
7

Just use two loops:

const size = 90;
for (let round = 0; round * size < maxPag; round++) {
  setTimeout(function(){
    for(let i = round * size; i < maxPag && i < (round + 1) * size; i++) {
       console.log(i);
       getInfoPage(i);
    }
  }, 15000 * round);
} 

Or use one loop that waits at every 90th iteration:

 const timer = ms => new Promise(res => setTimeout(res, ms));

 (async function() {
    for(let i = 0; i < maxPag; i++) {
       if(i && !(i % 90)) await timer(15000);
       getInfoPage(i);
    }
 })();

posted this

Have an answer?

JD

Please login first before posting an answer.