2015-11-08 34 views
-4

我試着迭代使用for循環的數組。在循環中有一個異步的ajax調用。我需要它使用for循環迭代的當前值,而不是在最後檢索ajax調用時更改的值。我搜索了一個解決方案,並偶然發現一個函數。奇怪的是,這似乎並沒有在這種情況下工作。任何人都可以幫忙?謝謝!異步javascript ajax調用關閉不起作用

編輯:
迭代的最後一個鍵值用於每個feed的標題。所以他提取了正確的feed(feed [key]),但是在成功部分,他只是爲每個feed推送last_key + title。我沒有得到任何錯誤,Ajax調用正常工作。

for (var key in feeds) { 
    (function(k) { 
    console.log('function: ' + k); // gives the correct keys, e.g. first time 'Reddit', second time 'Tweakers' 
    $.feedToJson({ 
     feed: feeds[k], 
     success: function(data) { 
     console.log('succes: ' + k); // always gives the last key, e.g. always 'Tweakers' 
     for (var i in data.item) { 
      var item = data.item[i]; 
      if (typeof item.title == 'object') { 
      news.push(k + ': ' + item.title[0]); 
      } else { 
      news.push(k + ': ' + item.title); 
      } 
     } 
     } 
    }); 
    })(key); 
} 

var feeds = { 
Reddit: 'https://www.reddit.com/r/leagueoflegends/.rss?sort=new', 
Tweakers:'http://www.tweakers.com/feeds/nieuws.xml' 

}

(function fetchNews() { 
    news = []; 
    for (var key in feeds) { 
     (function (k) { 
      console.log('function: ' + k); // gives the correct keys, e.g. first time 'Reddit', second time 'Tweakers' 
      $.feedToJson({ 
      feed: feeds[k], 
      success: function(data){ 
         console.log('succes: ' + k); // always gives the last key, e.g. always 'Tweakers' 
         for (var i in data.item) { 
          var item = data.item[i]; 
          if (typeof item.title == 'object') { 
           news.push(k + ': ' + item.title[0]); 
          } else { 
           news.push(k + ': ' + item.title); 
          }  
         } 
        } 
      }); 
     })(key); 
    } 

    setTimeout(function() { 
     fetchNews(); 
    }, 60000); 
})(); 

(function($) { 

$.extend({ 
    feedToJson: function(options, callback) { 
     if ($.isFunction(options)) { 
      callback = options; 
      options = null; 
     } 
     options = $.extend($.feedToJson.defaults,options); 
     var url = options.yqlURL + options.yqlQS + "'" + encodeURIComponent(options.feed) + "'" + "&_nocache=" + options.cacheBuster; 
     return $.getJSON(url, function(data){ 
       //console.log(data.query.results); 
       data = data.query.results; 
       $.isFunction(callback) && callback(data); //allows the callback function to be the only option 
       $.isFunction(options.success) && options.success(data); 
      }); 
    } 
}); 

})(jQuery的);

+1

「它似乎不工作」爲什麼?你看到了什麼結果? – JLRishe

+0

請提供更多信息。 '在這種情況下似乎不起作用' - 你會得到什麼?一個錯誤? 'feeds'數組和結果'news'數組中有什麼?阿賈克斯調用是否正確完成? – shershen

+0

迭代的最後一個關鍵值用於每個Feed的標題。所以他提取了正確的feed(feed [key]),但是在成功的部分,他只是爲每個feed推送last_key + title。 – Hans

回答

0

您可以使用$.each創建更簡單,更具可讀性的封閉。

$.each(feeds, function (key, feed) { 
    $.feedToJson({ 
     feed: feed, 
     success: function (data) { 
      console.log('succes: ' + key); // always gives the last key, e.g. always 'Tweakers' 
      $.each(data.item, function (_, item) { 
       if (typeof item.title == 'object') { 
        news.push(key + ': ' + item.title[0]); 
       } else { 
        news.push(key + ': ' + item.title); 
       } 
      }) 
     } 
    });  
}); 

基於代碼顯示您當前的閉合但是應該工作正常

+0

請注意,「閉環」問題的解決方案是*不*引入另一個閉包。它創造了範圍。是的,這正是'$ .each'給你的:每次迭代的範圍。 –