使用代码形式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);
答案 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>
答案 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>