2012-04-05 133 views
0
$(function() { 
    setInterval(function() { 
     $.get("refresh.php", function(result) { 
      $('#response').empty() 
      .hide() 
      .html(result) 
      $("#response").append(result) 
      .fadeIn("slow"); 
     }); 
    }, 5000); 
}); 

其實我的劇本打refresh.php每5秒在refresh.php我從MySQL數據庫提取數據,並創建一個模板:清除DIV內容

<table> 
<tr><td>Name<td><td>$fetch['name']</td></tr> 
</table> 

我在我的螢火蟲刷新檢查。 PHP5秒後發送響應只有一次,但在所有的瀏覽器就顯示了類似的結果兩次:

<table> 
    <tr><td>Name<td><td>$fetch['name']</td></tr> 
    </table> 
<table> 
    <tr><td>Name<td><td>$fetch['name']</td></tr> 
    </table> 

回答

4

它顯示了事半功倍的效果,因爲你把它有兩次:第一次使用html,然後使用append

$(function() { 
    setInterval(function() { 
     $.get("refresh.php", function(result) { 
      $('#response').empty() 
      .hide() 
      .html(result)     // <==== Here 
      $("#response").append(result) // <==== And here 
      .fadeIn("slow"); 
     }); 
    }, 5000); 
}); 

html替換元件與標記(或元素[S])您提供(有沒有必要使用它empty之前)的內容。 append附加您提供給元素的標記(或元素[s])。

你想要一個或另一個。我會跟html去:

$(function() { 
    setInterval(function() { 
     $.get("refresh.php", function(result) { 
      $('#response') 
       .hide() 
       .html(result) 
       .fadeIn("slow"); 
     }); 
    }, 5000); 
}); 
+1

感謝哎呀哥們 – 2012-04-05 21:49:14

-1

使用以下

$.get("refresh.php", function(result) { 

    $("#response").html(result); 
}); 
+0

爲什麼downvoted這個答案 – 2012-04-05 21:55:33