我想在5秒内将用户重定向到index.php,但它立即重定向到我。我不想在这个简单的代码中使用jQuery。setTimeout和window.location(location.href)不起作用
<script>
setTimeout(function(){location.href="index.php", 5000});
</script>
我想在5秒内将用户重定向到index.php,但它立即重定向到我。我不想在这个简单的代码中使用jQuery。setTimeout和window.location(location.href)不起作用
<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或父页面。
所以对于任何寻找一种方式来重定向5秒后,但在一个iframe中,并重定向TOP /父母页面以及这里是我如何实现了基于琼的回答原始问题。
<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>';
希望这可以帮助其他人寻找相同的解决方案。
感谢
移动'5000'后'}'... – 2013-03-27 09:07:51
@RobW - 这听起来像一个答案! – adeneo 2013-03-27 09:09:25
耶稣从我这里是愚蠢的@Rob W写这个作为我的问题的答案,所以我可以标记为解决方案 – FosAvance 2013-03-27 09:14:06