使用 jquery 延迟自动 url 重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677667/
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
Delay automatic url redirect with jquery?
提问by mark
I need a transition page that will display for 2 seconds before automatically redirecting to the home page. How do I do this? I can't seem to get delay to work for me.
我需要一个在自动重定向到主页之前会显示 2 秒的转换页面。我该怎么做呢?我似乎不能延迟为我工作。
回答by Nick Craver
You can just use setTimeout()
directly, like this:
你可以直接使用setTimeout()
,像这样:
setTimeout(function() {
window.location.href = "/NewPage.aspx";
}, 2000);
回答by JasCav
You could use jQuery Timer. Here is the code (also found in this article):
你可以使用jQuery Timer。这是代码(也可以在本文中找到):
// This will hold our timer
var myTimer = {};
// delay 2 seconds
myTimer = $.timer(2000, function() {
//redirect to home page
window.location = "/RedirectTimer/Home.aspx";
});
回答by Nithee
setTimeout(function(){ window.location = "/NewPage.aspx"; }, 2000);
回答by Tomalak
Would the delay()
functionnot work for you? Vanilla JavaScript with setTimeout()
would work equally well.
请问该delay()
功能不会为你工作?Vanilla JavaScript withsetTimeout()
也能很好地工作。
Hint: Suggesting actual code is kind of hard when you do not show your current code.
提示:当您不显示当前代码时,建议实际代码有点困难。