Jquery 使用ajax,如何仅在接收到的数据中有任何更新时获取通知

Jquery 使用ajax,如何仅在接收到的数据中有任何更新时获取通知,jquery,Jquery,最初,当它加载时,它不应该通知或提醒,但在每次超时后,如果数据中有任何更新,它应该提醒我。这正是它在聊天中的工作方式。有人能帮我吗,我对编程有点陌生……保存结果值并每次比较。大概是这样的: $(document).ready(function refreshText(){ $.ajax({ type: "POST", url: "user.php", data: "data1=1", success: function(resul

最初,当它加载时,它不应该通知或提醒,但在每次超时后,如果数据中有任何更新,它应该提醒我。这正是它在聊天中的工作方式。有人能帮我吗,我对编程有点陌生……

保存结果值并每次比较。大概是这样的:

$(document).ready(function refreshText(){
$.ajax({ 
        type: "POST",
        url: "user.php",
        data: "data1=1",
        success: function(result){
         $(".msg").html(result);
         setTimeout(refreshText, 5000);
         }});
    });

只需将最后一个结果存储到cookie中,并在ajax成功时进行检查:

$(document).ready(

    var last_result;

    function refreshText(){
        $.ajax({ 
            type: "POST",
            url: "user.php",
            data: "data1=1",
            success: function(result){

                 if(result == last_result){
                     // result is different
                     $(".msg").html(result);
                 }

                 last_result = result; // save the result

                 setTimeout(refreshText, 5000);
         }});
});

您可以检查ajax返回的结果,如:

成功:函数(结果){
如果(结果改变){
//做些手术
}
其他的
//做别的事

}
您所说的“在接收到的数据中更新”是什么意思?我的意思是,如果数据库中有任何更新,它应该通知或弹出一些内容。。。。。如聊天谢谢您的回答此代码每次都会提醒我,但我收到的数据没有任何更新..?这将仅在结果更改时提醒最初“结果”为0,它进入条件并在每次超时时提醒我。。。。
$(function(){
    refreshText();
});

var last_response; // define this variable to save the result
function refreshText(){
    $.ajax({ 
        type: "POST",
        url: "user.php",
        data: "data1=1",
        success: function(result){
            if( result === last_response ) return;
            last_response = result;
            $(".msg").html(result);
            setTimeout(refreshText, 5000);
        }
    });
}
 var Results = "";
 $(document).ready(function refreshText() {
     $.ajax({
         type: "POST",
         url: "user.php",
         data: "data1=1",
         success: function(result) {
             if (result != Results) {
                 result = Results;
                 alert("Change...");
             }
             $(".msg").html(result);
             setTimeout(refreshText, 5000);
         }
     });
 });