2012-12-03 48 views
0

無法弄清楚爲什麼我的循環複製結果? 我試圖從kalRss = rss url中獲取RSS提要。它獲得了一切,但它以奇怪的方式複製了條目。如果你看看螢火蟲控制檯,你可以選擇只有3個條目。 http://www8.umu.se/project/rut/display/index.html多個條目/帖子javascript谷歌飼料

<script src="https://www.google.com/jsapi" type="text/javascript"></script> 
<script type="text/javascript"> 
    google.load("feeds", "1"); 
    var kalRSS = "http://www.upc.umu.se/om-upc/kalendarium/prenumerera/kalender-rss/?categories=concert,conference,courseEducation,culturOnCampus,dissertation,exhibition,lecture,licentiateSeminar,meeting,theOthers,seminar,seminarSeries,workshop&whoCanAttend=employees&calendarIds=69&fromSiteNodeId=45897"; 
    function initialize() { 
     console.log("initialize"); 
     var container = $('#dailyFeed'); 
     container.empty(); 
     //var feed = new google.feeds.Feed("http://fastpshb.appspot.com/feed/1/fastpshb"); 
     var feed = new google.feeds.Feed("http://www.upc.umu.se/om-upc/kalendarium/prenumerera/kalender-rss/?categories=concert,conference,courseEducation,culturOnCampus,dissertation,exhibition,lecture,licentiateSeminar,meeting,theOthers,seminar,seminarSeries,workshop&whoCanAttend=employees&calendarIds=69&fromSiteNodeId=45897"); 
     feed.setNumEntries(3); 
     feed.load(function(result) { 
      if (!result.error) { 
       var html = ''; 
       var entryLenght = result.feed.entries.length; 
       console.log("Number of posts: " + entryLenght); 
       for (var i = 0; i < entryLenght; i++) { 
        console.log("Loop: " + i); 
        var entry = result.feed.entries[i]; 
        console.log(entry); 
        var startTime = entry.content.substr(11, 5) 
        var endTime = entry.content.substr(27, 5) 
        html += '<li class="daily">'; 
        html += '<h3 class="georgia">' + entry.title + '</h3>'; 
        html += '<h4>' + entry.categories[0] + '</h4>'; 
        html += '<p>Info: ' + entry.contentSnippet + '</p>'; 
        html += '<p><img src="img/watch.png" class="icon" />' + startTime + ' - ' + endTime + '</p>'; 
        html += '</li>'; 
        container.append(html); 
       } 
      } 
     }); 
    } 
    google.setOnLoadCallback(initialize); 
</script> 

回答

1

你正在做在你的循環中的html += "<li></li>"但你忘了這個變量復位內循環。所以在每次迭代中,環路其加入到容器中:

  • 項1
  • 項目1,項目2
  • 項目1,項目2,第3項

最終結果:

  • 項目1,項目1,項目2,第1項,第2項,第3項

要解決,在每次迭代的html變量重新設置"",例如:

for (var i = 0; i < entryLenght; i++) { 
    var html = ''; 
    // rest of your code 
} 
+0

非常感謝:) –