jQuery infinite loop over RSS elements
I'm using the jGFeed to retrieve RSS feed from distant server. Nothing hard, really. Issue I'm having is about the display of the feed : I'm trying to loop over each retrived element of the rss, and display it. Then remove it , and display the next one.
Here's how i'm trying to do so, without success :
$(document).ready(function() {
function loop(links){
var i = 0;
var arrayLength = links.length;
for (i=0;i<=arrayLength;i++){
$('#rssLink').empty().append(links[i]).fadeIn("slow");
setTimeout(functi开发者_JS百科on() {
$('#rssLink').fadeOut("fast");
}, 5000);
}
}
function animate(feeds){
var taille = feeds.length;
var links = [];
for ( var i = 0; i < taille; i++ ){
links[i] = "<a href='"+feeds[i].link+"'>"+feeds[i].title+"</a>";
}
loop(links);
}
$.jGFeed('http://www.wrc.com/services/newsrss.jsp',
function(feeds){
// Check for errors
if(!feeds){
// there was an error
return false;
}
animate(feeds.entries);
}, 50);
});
Looks to me like you're going to go one beyond the array length with this line:
for (i=0;i<=arrayLength;i++){
You should go while i < arrayLength.
Also, your 'loop' function is going to quickly add all the links in succession, clearing the #rssLink element each time. To iterate the array slowly, you could try something like:
function loop(links) {
function showLink(i) {
$('#rssLink').empty().append(links[i]).fadeIn("slow");
setTimeout(function() {
$('#rssLink').fadeOut("fast", function() {
if(i + 1 < links.length) showLink(i + 1);
else showLink(0); // this line causes it to loop again from the start
});
}, 5000);
}
showLink(0);
}
精彩评论