2015-05-10 27 views
-2
echo ' 
    <script type="text/javascript"> 
     setInterval(myfunction , 1000); 
     myfunction(){ 
      window.location = "../index.php"; 
     } 
    </script>'; 

如果我只是打電話window.location = "../index.php"沒有setInterval它確實有效我已經看過很多代碼,但沒有人可以幫助我謝謝你。如何設置php的setInterval?

+0

什麼是你想要做的setInterval? –

+0

去主頁這個「回聲」存在whitin一個「如果」。這適用於MySQL的諮詢。 –

+0

它不工作時調用setInterval?控制檯中是否有錯誤?你正確使用setInterval嗎?你可以嘗試用函數關鍵字定義myfunction,然後纔可以做到這一點。 –

回答

1

如果您想在一定時間後重定向或重新加載網頁,則只能在客戶端觸發事件。您可以使用java腳本和html元標記實現此目的。

1:使用HTML

<meta http-equiv="refresh" content="0";url=http://www.domain.com/new-page.html"> 

可以的含量值設置爲時間

2:使用JavaScript

<script type="text/javascript"> 

setInterval(function(){ 
window.location = "../index.php"; 
} , 1000); 


</script> 
+0

你能否擴展一下爲什麼我們需要'meta tag',在你回答中。 –

+0

您的JavaScript示例遭受與op相同的錯誤,您在函數定義之前沒有'function'關鍵字 –

+0

@PatrickEvans感謝您的支持。 –

0

1秒後重定向頁面。它運行一次

<script type="text/javascript"> 

setTimeout(function(){ 
window.location = "../index.php"; 
}, 1000); 

</script> 

如果你用PHP編寫,使用它

<?php 
//code here 
?> 
<script type="text/javascript"> 

setTimeout(function(){ 
window.location = "../index.php"; 
}, 1000); 

</script> 
<?php 
//code here 
?> 

,或者使用替代的setTimeout