[英]setTimeout and window.location (location.href) not working
我想在5秒内将用户重定向到index.php,但它会立即重定向我。 我不想在这个简单的代码中使用jQuery。
<script>
setTimeout(function(){location.href="index.php", 5000} );
</script>
这是正确的方法......
setTimeout(function(){location.href="index.php"} , 5000);
您可以在此处查看文档:
https://developer.mozilla.org/en/docs/DOM/window.setTimeout
句法 :
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]);
var timeoutID = window.setTimeout(code, [delay]);
示例:
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>
这也有效:setTimeout(“window.location ='index.php'”,5000);
我知道这个线程已经解决了,2年后我已经解决了这个问题,我个人刚刚使用了Joan的答案并修改了它以确切地解决了我需要它的方法,因为location.href不会重定向在iframe中调用时的TOP或Parent页面。
所以对于任何想要在5秒之后但在iframe内重定向并且重定向TOP / Parent页面的人来说,这是我如何根据Joan对原始问题的回答实现的。
<script type="text/javascript">
setTimeout(function(){window.top.location="index.php"} , 5000);
</script>
如果你想通过使用PHP来调用它,我个人在这里做的就是如何使用echo命令在5秒后重定向用户。
echo '<script type="text/javascript">setTimeout(function(){window.top.location="index.php"} , 5000);</script>';
希望这可以帮助其他人搜索相同的解决方案。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.