簡體   English   中英

Google Feed API行為

[英]Google feed api behaviour

我正在用Google Feed API進行實驗。 基本上,我想要我的提要列表以及提要條目的一些信息,以便處理通常在Google Reader中不可用的歷史提要。 我對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數組的值。

在我看來feed.load()是ajax調用,您正在傳遞一個回調函數,以在從請求返回時執行。 由於ajax是異步的,因此外層的for循環將繼續執行,因此,您始終會看到數組中的最后一個feed。

我相信在這種情況下關閉會有所幫助。

考慮將整個塊放在外部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");
}​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM