jQuery X 秒后使用 JavaScript 等待页面重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17150171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Page Redirect after X seconds wait using JavaScript
提问by virus
I need to redirect to specific url after 5 seconds after putting an error message. First i have used Javascript as below.
我需要在输入错误消息 5 秒后重定向到特定的 url。首先,我使用了 Javascript,如下所示。
document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000));
But it is not waiting for 5 seconds. Than I searched the issue in google than come to know that "document.ready()" is invoked when document is loaded at DOM, not at web browser.
但它不会等待 5 秒。比我在 google 中搜索这个问题之后才知道在 DOM 上加载文档时调用了“document.ready()”,而不是在 Web 浏览器上。
Than i have used window.load() function of jQuery but still i am not getting what i want.
比我使用过 jQuery 的 window.load() 函数,但我仍然没有得到我想要的。
$(window).load(function() {
window.setTimeout(window.location.href = "https://www.google.co.in",5000);
});
Can anyone please let me know how exactly i need to do to wait for 5 seconds.
任何人都可以请让我知道我需要怎么做才能等待 5 秒钟。
回答by ajtrichards
It looks you are almost there. Try:
看起来你快到了。尝试:
if(error == true){
// Your application has indicated there's an error
window.setTimeout(function(){
// Move to a new location or you can do something else
window.location.href = "https://www.google.co.in";
}, 5000);
}
回答by pala?н
You actually need to pass a function inside the window.setTimeout()
which you want to execute after 5000 milliseconds, like this:
您实际上需要window.setTimeout()
在 5000 毫秒后传递一个要在 5000 毫秒后执行的函数,如下所示:
$(document).ready(function () {
// Handler for .ready() called.
window.setTimeout(function () {
location.href = "https://www.google.co.in";
}, 5000);
});
For More info: .setTimeout()
更多信息: .setTimeout()
回答by shuseel
You can use this JavaScript function. Here you can display Redirection message to the userand redirected to the given URL.
您可以使用此 JavaScript 函数。在这里,您可以向用户显示重定向消息并重定向到给定的 URL。
<script type="text/javascript">
function Redirect()
{
window.location="http://www.newpage.com";
}
document.write("You will be redirected to a new page in 5 seconds");
setTimeout('Redirect()', 5000);
</script>
回答by Mohammad Adil
You need to pass a function to setTimeout
您需要将函数传递给 setTimeout
$(window).load(function () {
window.setTimeout(function () {
window.location.href = "https://www.google.co.in";
}, 5000)
});
回答by JoyGuru
Use JavaScript setInterval()
method to redirect page after some specified time. The following script will redirect page after 5 seconds.
使用 JavaScriptsetInterval()
方法在指定时间后重定向页面。以下脚本将在 5 秒后重定向页面。
var count = 5;
setInterval(function(){
count--;
document.getElementById('countDown').innerHTML = count;
if (count == 0) {
window.location = 'https://www.google.com';
}
},1000);
Example script and live demo can be found from here - Redirect page after delay using JavaScript
可以从这里找到示例脚本和现场演示 -使用 JavaScript 延迟后重定向页面
回答by sdespont
$(document).ready(function() {
window.setTimeout(function(){window.location.href = "https://www.google.co.in"},5000);
});
回答by ABHAY KUMAR
setTimeout('Redirect()', 1000);
function Redirect()
{
window.location="https://stackoverflow.com";
}
//document.write("You will be Redirect to a new page in 1000 -> 1 Seconds, 2000 -> 2 Seconds");
//document.write("您将在 1000 -> 1 秒后重定向到新页面,2000 -> 2 秒");
回答by Priyabrat Rath
<script type="text/javascript">
function idleTimer() {
var t;
//window.onload = resetTimer;
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
function logout() {
window.location.href = 'logout.php'; //Adapt to actual logout script
}
function reload() {
window.location = self.location.href; //Reloads the current page
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 1800000); // time is in milliseconds (1000 is 1 second)
t= setTimeout(reload, 300000); // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();
</script>