窗口位置替换 - 超时帮助?Javascript问题

问题描述 投票:0回答:1

我曾试图找出一种方法,在我的代码中添加超时之前,它重定向你.我已经上网搜索,并找到一些帮助,但他们没有做我所期望的。

****这将提示用户他们的出生年月,并计算年龄(大概) * ****如果用户未满18岁,将带他们去迪士尼乐园,如果没有,则允许他们进入 *。

function ageButton() { var askDOB = prompt("What year were you born?");

if (askDOB > 2002) {
    document.getElementById("agePass").innerHTML = "Access Denied"
    window.location.replace("http://www.disney.com");
}
else {
    document.getElementById("agePass").innerHTML = "Access Granted"
    window.location.replace("http://www.w3schools.com");
}

}

只需要在重定向前有3秒的超时时间。

javascript html
1个回答
1
投票

你可以使用setTimeout(function, milliseconds)来实现,就像这样。

// location replace in 1 second
setTimeout(function(){
    window.location.replace("http://www.disney.com");
}, 1000);
© www.soinside.com 2019 - 2024. All rights reserved.