Acknowledging that the setTimeout()
method is asynchronous and therefore starting with this:
for (var i = 1; i <= 3; i++) {
(function(i) {
setTimeout(function() {
console.log(i + " second(s) elapsed");
}, i * 1000);
})(i);
}
I'd like to replace console.log()
with something to the effect of
window.open("https://www.twitter.com","_self")
ordocument.getElementById("myFirstID").click();
and
window.open("https://www.facebook.com","_self")
ordocument.getElementById("mySecondID").click();
,
called alternatingly, each with a delay of, say, 5 minutes, but need some help replacing it without breaking it :-) Therefore I ask.
This was one early (but unsuccessful) attempt:
i=0;
while(i < 100)
{
setTimeout(function(){ window.open("https://www.bbc.com","_self") }, 3000);
setTimeout(function(){ window.open("https://www.cnn.com","_self") }, 3000);
i++
}
tldr;
also thought of using a switch()
statement
See also:
How to make setTimeout in while loop execute sequentially in JavaScript?
JavaScript closure inside loops – simple practical example
Calling two methods alternately after every 5 minutes.