2014-03-02 69 views

回答

1

在5000上設置運行功能更新格的時間間隔。 結帳以下鏈接: http://www.w3schools.com/js/js_timing.asp

比方說,你有與my_div ID的DIV,你想刷新它是由my_page.php要求每5秒鐘的內容。

客戶端JavaScript代碼:

<html> 
<head> 
</head> 
<body> 
<div id="my_div"></div> 
</body> 
    <script type="text/javascript"> 
    var refresh_time = 5000; 

    /** To make sure that program will execute when everything loaded in page. **/ 
    window.onload = function(){ 

    /** setInterval() will calls my_div_update() function in refresh times. **/ 
    setInterval(function(){my_div_update();}, refresh_time); 

    } 

    /** Uses Ajax request to update my_div content. **/ 
    function my_div_update() 
    { 
    var xmlhttp=new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function() 
     { 
     /** To make sure everything is ok on the way. **/ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
     document.getElementById("my_div").innerHTML=xmlhttp.responseText; 
     } 
     } 
    xmlhttp.open("GET","my_page.php"); 
    xmlhttp.send(); 
    } 
    </script> 
</html> 

服務器端PHP代碼:

<?php echo date('h:i:s - A'); ?> 
+0

這個工作,但就是這個好?我的意思是它每5秒刷新一次。是否有某種代碼用於檢查數據庫中是否有更改,然後將刷新 – Jan

+0

在http世界中,所有內容都以請求開頭,因此您必須向服務器頁面發送請求以查看更改。 –

+0

你能指導我在正確的方向如何做到這一點?或者給我一個教程? – Jan