倒计时后HTML HTML脚本重定向

时间:2020-07-15 18:48:54

标签: javascript html

使用代码形式here,在倒计时结束后如何重定向页面?

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>
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);

4 个答案:

答案 0 :(得分: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>
基本上只需添加window.location.href =“此处的html重定向文件的路径”;到if语句,然后添加要重定向到的路径。

答案 1 :(得分:0)

只需使用window.location方法

将您的Javascript代码更新为此

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";
}

答案 2 :(得分:0)

您可以通过分配给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>

答案 3 :(得分:0)

只需在if语句中添加location.replace()

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>