我正在寻找一些解决方案,比如php的.net观察者,我偶然发现了一段this代码,如下所示。我的应用程序应该等到数据库发生更改-新记录或现有记录状态的更改,然后在客户端页面上重新填充记录列表。问题是-我没有像下面的代码块那样使用txt文件,因此我可能不能使用filemtime来检查数据库中的更改或新记录。如何将其与数据库查询结果配合使用?
需要根据我的需求进行修改的代码块:
<script>
var timestamp = null;
function wait_for_message(){
$.ajax({
type: "GET",
url: "get_data.php?time_stamp=" + time_stamp,
async: true,
cache: false,
success: function (data){
var json - eval('(' + data + ')');
if(json['msg'] != "") {
alert(json['msg'] );
}
time_stamp = ['time_stamp'];
setTimeout('wait_for_message(), 1000);
}});
}
$(document).ready(function (){
wait_for_message();
});
</script>和php文件
<?php
$filename = dirname(__FILE__).'/data.txt';
$last_modif = isset($_GET['time_stamp']) ? $_GET['time_stamp'] : 0;
$current_modif = filemtime($filename);
while ($current_modif <= $last_modif){
usleep(1000);
clearstartcache();
$current_modif = filemtime($filename);
}
$response = array();
$response['msg'] = file_get_contents($filename);
$response['time_stamp'] = $current_modif;
echo json_encode($response);
?> 发布于 2012-09-17 23:03:34
在发现node.js之前,我就在使用这段代码
这是很久以前的事了,但是您可以简单地向表中添加一个名为time_stamp的列,并且当您添加一行时,将time()放入其中(或任何指示插入时间的内容)。
$last_modif = isset($_GET['time_stamp']) ? $_GET['time_stamp'] : 0;
$current_modif = $query("select `time_Stamp` from table order by time_stamp desc limit 1" );https://stackoverflow.com/questions/12461335
复制相似问题