如何在jQuery中加载新的rss feed项

时间:2011-12-13 14:50:41

标签: jquery jfeed jquery-xml

我正在使用jFeed来解析RSS提要,但我现在在想如何跟踪基于间隔的检查的更改。如何有效地检查Feed中是否有新项目与之前的加载相比?不幸的是,我不能使用RSS 2.0最后更新的参数,因为它不存在。

1 个答案:

答案 0 :(得分:1)

修正了它!请参阅以下解决方案:

//namespace for vars
$.feedentries = { 
    lastKnownEntry : ""
}; 

jQuery.getFeed({
    url: 'proxy.php?url='+encodeURIComponent(url),
    success: function(data) {
        //if new iterate through it until we've found the last know entry
        if(data.items[0].title!==$.feedentries.lastKnownEntry){
            var entries = "";
            $.each(data.items, function(i, feed) {
                if(feed.title!==$.feedentries.lastKnownEntry){
                    entries += '<div class="rss-entry">'+feed.title+'<div class="time"><a href="'+feed.link+'">'+feed.updated+"</a></div></div>\n";
                    //remove last element, prevents from overflowing
                    $(id+" .rss-entry:last-child").remove();
                } else {
                    //stop once we reach last known entry
                    return false;
                }
            });
            //insert into DOM
            $(id).prepend(entries);
            //write new last known rss title
            $.feedentries.lastKnownEntry = data.items[0].title;
        }
    }
});