0

I want to create a pop window and open a list of URLs in the browser. So when the first page loads in the pop up window, Wait for the page to load then open the next page... Go through the list of urls. To wait for the page to load, I use setTimeout. But I can't seem to make my code work, because of the for loop and the setTimeout. The window skips directlt to the last url in the list. Is there any other way I can make it work? Thank you,

Here's my code:

// exammple of list urls
var my_list = ['https://www.youtube.com/watch?v=77kOn5NZBHw',
  'https://www.youtube.com/watch?v=1IRo21UhvZg',
  'https://www.youtube.com/watch?v=yRJxz0NicgI'
]

// open a new pop up window
var response = window.open(my_list[0], 'new', true);

for (let i = 0; i < my_list.length; i++) {
    setTimeout(function() {
    console.log(response.location.href);
    response.location = my_list[i];
  }, 3000)

};
8
  • You can obviously format your code any way you like for your own purposes, but when asking people for help, it's only courteous to take the time to format the code in a readable, consistent way. Commented Apr 22, 2022 at 8:07
  • You've only a single window of which you're changing the location. Commented Apr 22, 2022 at 8:07
  • You could use the onload event of window instead of a timeout. Depending on the URL of course. Commented Apr 22, 2022 at 8:24
  • @T.J.Crowder Sorry I wrote that code in the console's browser not in an editor. I'll fix it! Commented Apr 22, 2022 at 9:40
  • @Lain I see what you mean with using onload, but i genuinely want to wait 3000 for example after the page is loaded. Ths problem with thesetTimeout inside the for loop is that it executes the for loop then setTimeout. Thus fetching the last index returned by the for loop. Commented Apr 22, 2022 at 11:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.