2012-02-23 12 views

回答

2

我不認爲你會發現,具體的教程,但你只需要學習AJAX然後進行AJAX使用JavaScript的setInterval方法調用每兩分鐘。


編輯

咩,我已經夠無聊寫這個例子。這沒有經過測試,但我不認爲它有錯誤。

<html> 
<head> 
    <script type="text/JavaScript"> 
     window.onload = function() 
     { 
      // call your AJAX function every 2 minutes (120000 milliseconds) 
      setInterval("getRecords()", 120000); 
     }; 

     function getRecords() 
     { 
      // create the AJAX variable 
      var xmlhttp; 
      if (window.XMLHttpRequest) 
       xmlhttp = new XMLHttpRequest(); 
      else 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

      // set up the response function 
      xmlhttp.onreadystatechange = function() 
      { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
       { 
        /* 
         Your code goes here. 'xmlhttp.responseText' has the output from getRecords.php 
        */ 
        document.getElementById("txaRecords").innerHTML = xmlhttp.responseText; 
       } 
      } 

      // make the AJAX call 
      xmlhttp.open("GET", "getRecords.php", true); 
      xmlhttp.send(); 
     } 
    </script> 
</head> 
<body> 
    <textarea id="txaRecords"></textArea> 
</body> 
</html> 
+0

非常感謝你。有用! – jaypabs 2012-02-23 15:53:04

+0

@ user1034801:如果有效,請將其標記爲您接受的答案。謝謝! – Travesty3 2012-02-23 17:07:03

+0

我不能投票。它說:投票需要15點聲望。 – jaypabs 2012-02-23 23:29:52

1

這是我爲這個確切目的編寫的一些代碼。酌情適應。

AJAX代碼:

function timer() 
{ 
var t=setTimeout("check()",2000); 
// At an appropriate interval 
} 


function check(){ 

     var xmlhttp; 

     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
      if (xmlhttp.responseText!=""){ 
       var output = xmlhttp.responseText; 
       // Do what you need to do with this variable 
       } 
      } 
      } 
     xmlhttp.open("GET","backend.php",true); 
      // Set file name as appropriate. 
     xmlhttp.send(); 
     timer(); 
     } 

PHP代碼:

<?php 

// This assumes you have already done mysql_connect() somewhere. 

// Replace as appropriate 
$query = "SELECT * FROM table_name"; 

// Perform the query 
$result = mysql_query($query); 

// Get the results in an array 
while($row = mysql_fetch_array($result)) { 
    // Echo the message in an appropriate format.     
    echo "<br />" . $row['column_name']; 
} 
?> 

記住要啓動的JS功能之一爲您加載頁面:

<body onload="timer()">