< /li >\" that is exactly the same as th" />
开发者

Only append if content is new

开发者 https://www.devze.com 2023-01-23 08:38 出处:网络
I\'m trying to use the append content to make an live feed with jQuery but my problem is that it append more content, even if there already is an \"< li >< /li >\" that is exactly the same as th

I'm trying to use the append content to make an live feed with jQuery but my problem is that it append more content, even if there already is an "< li >< /li >" that is exactly the same as the one that is already there...

My current code: http://pastebin.com/y开发者_运维知识库EtSsg90

Would be great if someone could post an example-code with comments :)


Try this starting right after the comment on line 12:

repeat = false;
jQuery('ul#feed li').each(function() {
    if($(this).text() == data)
    {
        repeat = true;
    }
});

if(!repeat)
{
    jQuery('ul#feed').prepend('<li>' + data + '</li>');
}

it's looking for existing #feed li's that have the same text as data. If one exists, skip adding the data; otherwise add it like normal.


You need to keep track of which entries you have already added. For example, if each entry has an id number, you could maintain a list of all the ids added, or just the highest id, it they're sequential.

You could also do this on the server side. With the request, you could pass the id of the highest id you have added to only get new entries. If you have a timestamp for each entry, you could also use that to handle the filtering.


var exists = false,
    insertation = 'feedText..';

$('li').text(function(index,text) {
    if(text == insertation) exists = true;
});

if(!exists) //perform the appending
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号