2012-04-05 145 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