1

我正在使用 google feed api。基本上我想要一个我的提要列表以及提要条目的一些信息,以便处理通常在谷歌阅读器上不可用的历史提要。我对 google feed api 的一种行为感到困惑,我在下面的代码中将其记录为注释。我确信我没有得到任何东西,因为我是 js 新手。

这是我的问题作为评论的代码:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" charset="UTF-8"  >


    google.load("feeds", "1");

   /* Here I define the list of my feeds */

   var feeds=['MYFEED1', 'MYFEED2', 'AND SO ON'];

    function initialize() {
      for (var j=0;j<feeds.length;j++) {
        var feed = new google.feeds.Feed(feeds[j]);
        feed.includeHistoricalEntries();
        feed.setNumEntries(100);

        feed.load(function(result) {
            if (!result.error) {
            var container = document.getElementById("out");
            var furl = result.feed.feedUrl;
            var flink = result.feed.link;
            for (var i = 0; i < result.feed.entries.length; i++) {  
                var entry = result.feed.entries[i];

                var div = document.createElement("div");

                /* HERE IS WHERE I DON'T GET IT. 
                                feeds[j] always just shows the last feed in the array. 
                                But furl always shows the correct feedUrl
                Though if I uncomment alert("NEXT") few lines below 
                                thus giving a short pause, feeds[j] correctly 
                                shows the same url as feedUrl. Why?  */
                div.appendChild(document.createTextNode(feeds[j]+" "
                             +furl+" "+flink+" "+ entry.link + " " + entry.title)); 
                container.appendChild(div);
          }
        }
      });
     //alert("NEXT");
    }
}

    google.setOnLoadCallback(initialize);

    </script>
  </head>
    <body><div id="out" ></div>
  </body>
</html>

另外我想知道是否有一种方法可以通过查询 mysql 数据库来设置 feeds 数组的值。

4

1 回答 1

1

在我看来 feed.load() 是一个 ajax 调用,并且您正在传递一个回调函数以在从请求返回时执行。因为 ajax 是异步的,所以外部 for 循环会继续执行,因此您总是会看到数组中的最后一个提要。

我相信闭包应该在这种情况下帮助你。

考虑将整个块放在外部 for 循环中的单独函数中并使用闭包。像这样的东西:

function initialize() {
for (var j = 0; j < feeds.length; j++) {
    (function(val) { //closure begin
        var j = val;
        var feed = new google.feeds.Feed(feeds[j]);
        feed.includeHistoricalEntries();
        feed.setNumEntries(100);

        feed.load(function(result) {
            if (!result.error) {
                var container = document.getElementById("out");
                var furl = result.feed.feedUrl;
                var flink = result.feed.link;
                for (var i = 0; i < result.feed.entries.length; i++) {
                    var entry = result.feed.entries[i];

                    var div = document.createElement("div");

                     /* HERE IS WHERE I DON'T GET IT. 
                            feeds[j] always just shows the last feed in the array. 
                            But furl always shows the correct feedUrl
            Though if I uncomment alert("NEXT") few lines below 
                            thus giving a short pause, feeds[j] correctly 
                            shows the same url as feedUrl. Why?  */
                    div.appendChild(document.createTextNode(feeds[j] + " " + furl + " "  + flink + " " + entry.link + " " + entry.title));
                    container.appendChild(div);
                }
            }
          }
        })(j); //Closure end
    });
    //alert("NEXT");
}​
于 2012-11-18T19:28:13.663 回答