0

So I have a for loop and there is one line of code in there that opens a URL for each other iterations. I would like that line that opens the URL to wait 2 seconds before opening each one. How would I do it?

I tried the setTimeout function, but it iterates through the whole loop instantly after waiting the specified seconds, but I want it to wait for each iteration, not just before the iteration or during the first one.

The structure of my code looks something like this:

function someFunction(){
// do something
for(i = 0; i < range; i++){
//do something
//**open URL**
//do something
   }
}

How would I make it wait 2 seconds for every iteration before executing that one specific line where it opens the URL? None of the other questions seem to help me, so I was wondering if anyone could help.

btrballin
  • 1,420
  • 3
  • 25
  • 41
  • Why on earth would you ever want to do that? Whats the use case here? – mike510a Oct 30 '16 at 17:49
  • @mike510a My program opens multiple URLs at once and I just want a small delay so the website server doesn't have to process all the requests simultaneously. – btrballin Oct 30 '16 at 17:52
  • Maybe this link will help : http://stackoverflow.com/questions/36637772/how-to-settimeout-inside-a-loop – pwet Oct 30 '16 at 17:56

3 Answers3

1

You can use settimeout

function delayedFunction(counter){
  counter--;
  console.log(counter);
  if(counter){
    setTimeout(function(){delayedFunction(counter); }, 1000);
  }
}

delayedFunction(5);
Alexandr
  • 866
  • 8
  • 13
1

You cannot do this in a for loop. You can do this with setInterval(). The setInterval() method will continue until clearInterval() is called.

Structure

var interval = setInterval(function() {
    var i = 0; 
    if(i < x) {
        ...
    } else {
        clearInterval(interval);
    }
}, ms);

Example

var urls = ["url1", "url2", "url3", "url4"];

function showDelayed(arr, ms) {
  var i = 0;
  var interval = setInterval(function() {
    if (i < arr.length) {
      // Do something here
      console.log(arr[i]);
    } else {
      clearInterval(interval); // Clear when i === arr.length
    }
    i += 1; // Interval increments 1
  }, ms);
}

showDelayed(urls, 300);
0

Not that this is a very good practice (opening up multiple URLS on an interval), but since you asked.

    var urlarray = ["https://www.mysite1.com", "https://www.mysite2.com", "https://www.mysite3.com"];

    var currentURL = 0;

    setInterval(function() {

      if (currentURL < urlarray.length) {
        alert(urlarray[currentURL]);
      }
      currentURL++;
    }, 1500);
mike510a
  • 2,102
  • 1
  • 11
  • 27
  • Note that this will not stop the timer from going after it reaches the end -- but it will not open any more windows.. the timer will actually continue to run until you do `clearTimeout()` properly – mike510a Oct 30 '16 at 17:55
  • There is most certainly a better way to do whatever it is that you are doing by opening up multiple URLS -- whether it is EventSource or ads, theres a better way... – mike510a Oct 30 '16 at 18:01
  • Depends on what the purpose of the script is really – mike510a Oct 30 '16 at 18:03
  • Basically, the URL appends a date. By incrementing just the date part of the URL and opening it, it performs some action for all those days. What that action is not important for this question – btrballin Oct 30 '16 at 18:13