1

I want to wait on every iteration of the loop except the 1st one. This is my code which is working fine for 1st iteration after that setTimeout function wait for some x seconds and run all the iterations at once without wait.

Here is the code

var requests_made = 0;
drivers.forEach(function(driver) {
    if (requests_made == 0) {
        createUser(data);
    } else {
        setTimeout(function () {
            createUser(data);
        },30000);
    }
    requests_made++;
});
5
  • Why do you want to wait 30 seconds between each operation? Commented May 3, 2017 at 14:51
  • @ibrahimmahrir hes already incrementing the requests_made var at the end of each loop, so by the second loop it would already be 1. Commented May 3, 2017 at 14:57
  • The code is doing exactly what you ask for. It is setting a timeout on the call to createUser for 30 seconds for the 2nd through the nth iteration of the loop. The forEach still goes as quickly as possible though each loop so they all get set to execute in 30 seconds within a few milliseconds of each other. Please tell us what you are actually trying to do with the delay and maybe we can help. Are you trying to make each call to createUser() 30 seconds apart? Commented May 3, 2017 at 15:00
  • No it is not working as required. It call the first functions quickly and the remainings nth call right after waiting for x seconds. I want to wait for x seconds for every iteration of loop. Commented May 3, 2017 at 17:14
  • Actually the drivers array has sorted driver so I want to send request to nearest drivers first then a delay then to second driver a delay then to third and so on to n driver Commented May 3, 2017 at 17:15

1 Answer 1

6

Well your timeout uses a static delay value wich is 30000, which will be used by all the iterations, so they will all start after 30 seconds.

This delay should be dynamic and increase dynamically along with the iterated index, here's what you will need:

var requests_made = 0;
var drivers = [10, 50, 30, 40, 50];
drivers.forEach(function(driver, index) {
  if (requests_made == 0) {
    //createUser(data);
    console.log(index);
  } else {
    setTimeout(function() {
      //createUser(data);
      console.log(index);
    }, 1000 * index);
  }
  requests_made++;
});

Note:

I used an array of numbers and reduced the delay value for testing purposes.

Sign up to request clarification or add additional context in comments.

8 Comments

Will it delay for every iteration and then call a function?
No but the delay value will increase by 1000 in each iteration, as if it waits for the previous iteration then make a new delay.
It will work like I want ? My problem is to wait on each iteration of loop then call the function. So the solution you proposed will wait on each iteration and call the function right ?
and What if I need constant delay for all ?
but still the desired output is not achived
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.