javascript setTimeout 和 window.location (location.href) 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15655073/
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
setTimeout and window.location (location.href) not working
提问by FosAvance
I want to redirect user to index.php in 5 seconds, but it redirects me right away. I don't want to use jQuery in this simple code.
我想在 5 秒内将用户重定向到 index.php,但它立即重定向了我。我不想在这个简单的代码中使用 jQuery。
<script>
setTimeout(function(){location.href="index.php", 5000} );
</script>
回答by Joan Caron
This is the right way...
这是正确的方法...
setTimeout(function(){location.href="index.php"} , 5000);
You can check the docs here:
您可以在此处查看文档:
https://developer.mozilla.org/en/docs/DOM/window.setTimeout
https://developer.mozilla.org/en/docs/DOM/window.setTimeout
Syntax :
句法 :
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]);
var timeoutID = window.setTimeout(code, [delay]);
Example :
例子 :
WriteDatePlease();
setTimeout(function(){WriteDatePlease();} , 5000);
function WriteDatePlease(){
var currentDate = new Date()
var dateAndTime = "Last Sync: " + currentDate.getDate() + "/"
+ (currentDate.getMonth()+1) + "/"
+ currentDate.getFullYear() + " @ "
+ currentDate.getHours() + ":"
+ currentDate.getMinutes() + ":"
+ currentDate.getSeconds();
$('.result').append("<p>" + dateAndTime + "</p>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="result"></div>
回答by Geert
This also works: setTimeout ("window.location='index.php'", 5000);
这也有效: setTimeout ("window.location='index.php'", 5000);
回答by Birdy
I know this thread has already been solved and its somewhat 2 years later that i have come accross this, I personally just used the example from Joan's answer and modified it to work exactly how i need it to as the location.href will not redirect the TOP or Parent page when called within an iframe.
我知道这个线程已经解决了,大约 2 年后我遇到了这个问题,我个人只是使用了琼的答案中的例子,并修改了它以完全按照我需要的方式工作,因为 location.href 不会重定向在 iframe 中调用时的 TOP 或父页面。
So for anyone looking for a way to redirect after 5 seconds but within an iframe and to redirect the TOP / Parent page aswell here is how i have achieved that based on Joan's answer to the Original Question.
因此,对于任何想在 5 秒后但在 iframe 内重定向并重定向 TOP / Parent 页面的人来说,这里是我如何根据 Joan 对原始问题的回答来实现这一目标的。
<script type="text/javascript">
setTimeout(function(){window.top.location="index.php"} , 5000);
</script>
And if you wanted to call this by using PHP as i personally did do here is how you would use the echo command to redirect the user after 5 seconds.
如果您想使用 PHP 调用它,就像我个人在这里所做的那样,您将如何使用 echo 命令在 5 秒后重定向用户。
echo '<script type="text/javascript">setTimeout(function(){window.top.location="index.php"} , 5000);</script>';
Hope this helps anyone else searching for the same solution.
希望这可以帮助其他人寻找相同的解决方案。
Thanks
谢谢