当我不在 load.php 文件中使用 if 条件($last_msg_id_db>$last_msg_id) 时,jquery 函数可以正常工作,并在每半秒后继续检查数据。
但是,如果我使用 if 条件,jquery 函数仅在第一次运行,但在第一次运行后,它不起作用并停止工作。
我认为,我没有将 load.php 中的 if 条件放在正确的位置。请帮助或建议任何替代方法。
php 文件(Load.php)在这里
 $last_msg_id=$_POST['last_msg_id'];
$sql=mysqli_query($db3->connection,"SELECT * FROM chat_com ORDER by id ASC");
$sql_m=mysqli_query($db3->connection,"SELECT max(id) as maxid  FROM chat_com");
$row_m=mysqli_fetch_array($sql_m);
$last_msg_id_db=$row_m['maxid'];
if($last_msg_id_db>$last_msg_id){
    while($row=mysqli_fetch_array($sql)){
        $textt=$row['mesg'];
        $textt2="$textt<br>";
        $response=array();
        $response['msg']=$textt2;
        $response['last_msg_id_db']=$last_msg_id_db;
        $final_response[]=$response;
    }
}
echo json_encode($final_response);
Jquery 函数在这里。
var last_msg_id = 1;
function chat_com_one(id, name) {
  $('#chatcom').show('fast');
  (function chatcom_load_one(id, name) {
    alert(last_msg_id);
    $.post('load.php', {option:'chatcom_load_one', last_msg_id:last_msg_id},     function(data) {
        var json = eval('(' + data + ')');
        $.each(json, function(i, row) {
            $("#chatcom #commid #commidwin").append(row['msg']);
            last_msg_id = row['last_msg_id_db'];
        });
        setTimeout(chatcom_load_one(id, name), 500);
    });
}(id, name));
$('#chatcom_send').click(function() {
    $.post('send.php', { option:'chat_com_send_one', text:$('#chatcom_text').val(), tocom:id}, function(data) {
        document.getElementById('chatcom_text').value = '';
    });
});
}