0

using the code form here how could I redirect the page after the countdown ends?

var timeleft = 10;
var downloadTimer = setInterval(function(){
  if(timeleft <= 0){
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
  } else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
  }
  timeleft -= 1;
}, 1000);
<div id="countdown"></div>

JB945816
  • 11
  • 2
  • 1
    Does this answer your question? [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – Daniel_Knights Jul 15 '20 at 18:53

4 Answers4

1

var timeleft = 10;
var downloadTimer = setInterval(function(){
  if(timeleft <= 0){
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
    window.location.href = "Path to html redirect file here";
  } else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
  }
  timeleft -= 1;
}, 1000);
<div id="countdown"></div>
Basically just add window.location.href = "Path to html redirect file here"; to the if statement and add the path that you want to redirect to.
1

just add a location.replace() in your if statement

var timeleft = 10;
var downloadTimer = setInterval(function(){
  if(timeleft <= 0){
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
    location.replace("https://www.stackoverflow.com")
  } else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
  }
  timeleft -= 1;
  
  
}, 1000);
<div id="countdown"></div>
DCR
  • 14,737
  • 12
  • 52
  • 115
0

Just use window.location method

Update your Javascript code to this

if(timeleft <= 0){
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
    //the complete URL where you want to redirect
    window.location = "https://www.exapmle.com/"
} else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
}
Rupesh Chaudhari
  • 308
  • 2
  • 3
  • 12
0

You can do a redirect by assigning to window.location.href.

let timeleft = 10;
const downloadTimer = setInterval(function(){
  if (timeleft <= 0) {
    clearInterval(downloadTimer);
    document.getElementById("countdown").innerHTML = "Finished";
    window.location.href = 'https://www.youtube.com/dQw4w9WgXcQ';
  } else {
    document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
  }
  timeleft -= 1;
}, 1000);
<div id="countdown"></div>
laptou
  • 6,389
  • 2
  • 28
  • 59