0

我需要加载尚未加载的新消息并将它们添加$.append到 div,但我不知道如何。
最好的方法是到时候检查它。var chatdata = null;

$.ajax({
    type: "GET",
    data: {id: id},
    url: "/chatbin/receive-bin.php",
    dataType: "json",
    cache: false,
    async: true,
    contentType: "application/json",
    success: function(data) {
        chatdata = "";
        $('#chattext').html('');
        $.each(data.messages, function(i,dat){
            chatdata += "<div class='message'><img class='chatuserimg' src='/assets/userpics/" + dat.id + "_64.png'><span class='userdname'>" + dat.dname + "<span class='time'>" + dat.time + "</span></span><div class='message_text'>" + dat.message + "</div></div>\n";
        });

        gotoBottom();
        $('#chattext').append(chatdata);
        gotoBottom();

        c_id = id;
        autofocus();

        setInterval(checkNewmsg, 1000);
    },
    error: function(xhr, status, error) {
        alert('There was an error while sending the request, please try again later.');
   }
});

JSON文件:

{"messages": [
{"dname":"Person1", "id":"1", "message":"Hi", "time":"25.06.2013, 14:49"}, 
{"dname":"Person2", "id":"2", "message":"Cheers", "time":"25.06.2013, 14:50"}
]}
4

1 回答 1

0

如果时间是消息的唯一标识符,则播放时间

$.ajax({
    type: "GET",
    data: {id: id},
    url: "/chatbin/receive-bin.php",
    dataType: "json",
    cache: false,
    async: true,
    contentType: "application/json",
    success: function(data) {
        chatdata = "";
        $('#chattext').html('');
        $.each(data.messages, function(i,dat){

          //play with class here -- if time is unique identifier of the message
          var class = date.time.replace(/\./g,"-").replace(/\:/g,"-").replace(/\,\s/g,"-");

           if(!$('.message').hasClass(class))
           {
             chatdata += "<div class='message "+class+"' ><img class='chatuserimg' src='/assets/userpics/" + dat.id + "_64.png'><span class='userdname'>" + dat.dname + "<span class='time'>" + dat.time + "</span></span><div class='message_text'>" + dat.message + "</div></div>\n";
           }
        });

            gotoBottom();
            $('#chattext').append(chatdata);
            gotoBottom();

            c_id = id;
            autofocus();

            setInterval(checkNewmsg, 1000);
    },
    error: function(xhr, status, error) {
        alert('There was an error while sending the request, please try again later.');
    }
});
于 2013-06-27T14:08:36.733 回答