使用Ajax仅加载新数据

I need to load only new data into my div with ajax. At the moment I'm currently loading all data, because if I delete a record in the database it also removes it from my chat div.

Here is my js code:

var chat = {}
chat.fetchMessages = function () {
    $.ajax({
        url: '/ajax/client.php',
        type: 'post',
        data:  { method: 'fetch', thread: thread},
        success: function(data) {
            $('.chat_window').html(data);
        }
    });
}    

chat.throwMessage = function (message) {
    if ($.trim(message).length != 0) {
        $.ajax({
            url: '/ajax/client.php',
            type: 'post',
            data:  { method: 'throw', message: message, thread: thread},
            success: function(data) {
                chat.fetchMessages();
                chat.entry.val('');
            }
        });
    }
}

chat.entry = $('.entry');

chat.entry.bind('keydown', function(e) {
    if(e.keyCode == 13) {

        if($(this).val() == ''){
        } else {
            chat.throwMessage($(this).val());
            e.preventDefault();
        }

    }
});

chat.interval = setInterval(chat.fetchMessages, 8000);
chat.fetchMessages();

I have had a look around and some say that if you pass a timestamp to the server and load new content that way, but I can't seem to get my head around that. If you need php let me know.

Right, so the timestamp thing makes the most sense. You'll need to do a few things:

  1. On the back end, you need to make client.php accept a timestamp parameter in the querystring. When returning data, instead of just returning all of it, make it return everything since the time stamp, if given. Otherwise return everything.
  2. The very first time you load the chat client, the first thing you should do is make an Ajax call to a new PHP file that returns the current server timestamp. Store the value of that in a Javascript variable as a Number.
  3. During chat.fetchMessages(), increment the value of the timestamp variable by however long it's been since the last fetch (looks like 8000 milliseconds), and feed that to client.php, like url: '/ajax/client.php?timestamp=' + latestFetchTimestamp,
  4. Instead of replacing all HTML content, append instead.