Dylan
Dylan

Reputation: 137

How can I open a URL periodically with JavaScript?

Javascript array:

var urls = ['https://www.google.com', 'https://www.msn.com', 'https://stackoverflow.com'];

I am having the array of URLs. I want to open each URL every 15 seconds in a new tab or window.

function g(){
  setTimeout(o, 1);
}
function o(){
  window.open('page.php');
}

The above code is not working.

Upvotes: 1

Views: 345

Answers (1)

Jaydeep Mor
Jaydeep Mor

Reputation: 1703

I think you are finding setInterval.

var urls     = ['https://www.google.com', 'https://www.msn.com', 'https://stackoverflow.com'];
var index    = 0;
var interval = setInterval(o, 15000);

function o() {
    if (typeof urls[index] !== typeof undefined) {
        window.open(urls[index]);
        index++;
    } else {
        clearInterval(interval);
    }
}

Fiddle

Upvotes: 2

Related Questions