簡體   English   中英

setTimeout和window.location(location.href)不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM