0

I'm trying to show diffrent urls after a specific time. I tried this but it looks that only the result of location.href is considered (as if the function ignores the other lines), can someone please explain why it behaves this way?

var stack = ["https://stackoverflow.com", "https://youtube.com", "website3"];
function myFunction() {
location.href = stack[0];
setTimeout(function() {window.location.replace = stack[1];}, 3000);
setTimeout(function() {window.location.replace = stack[2];}, 3000);
}
<button onclick="myFunction()">change website</button>
Yosra MH
  • 129
  • 3
  • 11
  • 2
    changing the location will load the other page and UNLOAD all scripts on current page. Nothing on your page will be executed after the location change. you would need an iFrame or open in a new tab – mplungjan Apr 06 '21 at 14:50
  • 1
    If it DID work, both of the statements would be executed 3 seconds after they were executed – mplungjan Apr 06 '21 at 14:52
  • @mplungjan sounds legit, thank you so much :) – Yosra MH Apr 06 '21 at 14:55

1 Answers1

1

When you run location.href, it is redirecting your user to the first url of the stack, so it doesn't get to run the next lines.