views:

33

answers:

1

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(function() {
                    $('#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);
    }); 
+1  A: 

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);
  }
sje397
You solution works quite well, yet, the loop isn't infinite... I've tried checking your code, adding console.log to try have a sneak peak to whats going on, but nothing seems wrong ... :-/
pixelboy
@pixelboy - i think i misunderstood. to continue to loop over the feed repeatedly, see the commented line above
sje397
yep, that's what I came up with too. Thanks.
pixelboy