只看加粗的字體
js 部分
1: ajax 成功回調函數中 一定要用時間函數間隔調用 get_comment().
get_comments('init');
function get_comments(msg){
if(msg == undefined){
msg = '';
}
var post_id ="{$id}";
$.ajax({
type: "GET",
url: "",
data: {},
dataType: "json",
success: function(data){
// 這里 你自己處理 自己的data.
setTimeout("get_comments()",60000);
}
})
}
php部分
1: 根據條件,取得數據的條數,取名 old_counts
2: 根據 while(true)循環,再次查詢數據庫的條數,new_counts;
3: 新的條數和舊地條數做比較。比較結果不一樣,返回結果。
4: break ,跳出循環
5 : $time_count 是 對於一定的時間內,數據沒有更新,也跳出循環。
public function get_comment(){
$post_id = I('get.post_id',0,'intval');
$table = I('get.table');
$msg = I('get.msg');
$comment_model=D("Common/Comments");
$comments=$comment_model->where(array("post_table"=>$table,"post_id"=>$post_id,"status"=>1,'user_type'=>2))->order("createtime ASC")->select();
$OldCommentData = count($comments);
if($msg=='init'){
if(!empty($comments)){
$comments =self::forData($comments);
}
echo json_encode($comments);exit;
}
$time_count =0;
while(true){
set_time_limit(0);// 函數允許腳本運行的時間。設為0,程序不受限制。
$time_count++;
$newComments=$comment_model->where(array("post_table"=>$table,"post_id"=>$post_id,"status"=>1,'user_type'=>2))->order("createtime ASC")->select();
$newCommentData = count($newComments);
if($newCommentData != $OldCommentData){
if(!empty($comments)){
$newComments =self::forData($newComments);
echo json_encode($newComments);
break;
}
}
usleep(1000);
//一定的時間后沒有數據變化也跳出
if($time_count >= 800){
$data = "";
echo json_encode($data);
break;
}
}
}