2014-10-07 33 views
0

在我的情况下,我会在X秒后自动刷新。但是我想只在插入或更新新数据时刷新它。只有当新数据插入或更新时才自动刷新/加载数据

这是我的脚本。

refresh.php

<script src="jquery-1.4.js"></script> 
<script> 
$(document).ready(function() { 
$("#responsecontainer").load("refreshnow.php"); 
var refreshId = setInterval(function() { 
$("#responsecontainer").load('refreshnow.php?randval='+ Math.random()); 
}, 1000); 
}); 
</script> 

<div id="responsecontainer"></div> 

refreshnow.php

<?php 
$query = mysqli_query($connection,"select * from `table`"); 
$rows = mysqli_num_rows($query); 
?> 
<table style="background:#F0F0F0;" border=1 style="border-collapse:collapse"> 
<tr> 
    <th>Name</th> 
    <th>Job</th> 
</tr> 
<tbody> 
<?php 
while($result = mysqli_fetch_array($query)){ 
    echo "<tr>"; 
    echo "<td>".$result["name"]."</td>"; 
    echo "<td>".$result["job"]."</td>"; 
    echo "</tr>"; 
} 
?> 
</tbody> 

所以,可以在插入或更新,没有新的数据,才有可能自动加载新数据刷新x秒后?

+1

你就必须打破负荷()成一个正常的AJAX调用,这样就可以比较最后取到当前和丢弃相同的回报。 – dandavis 2014-10-07 06:37:04

回答

0

如果你想使用你现有的代码,只需在mysql表中添加另一列(如果你有一个配置或设置表,这将是easyer,如果不是插入每个记录的广告时间戳)与值time(),这会在每次插入/修改行时更新数据库中的时间戳。 然后就做这样的查询:

"select timestamp_column from `settings_table` order by timestamp_column limimt 1" 

If ($result["timestamp_column"]+1000>=time()){ 
     //do the refreshnow.php code here 
} 

希望它能帮助, 干杯

相关问题