我想從多個Ajax請求的返回中獲得總數,現在我使用同步來使它工作,因爲我認爲這將是一個更好的解決方案。從多個Ajax請求中獲取總數
這裏是我的咖啡
get_total = (trend, duration) ->
total = 0
for keyword in trend.search_terms
url = "http://otter.topsy.com/search.json?q=#{keyword}&window=#{duration}"
$.ajax
url: url
async: false
success: (data) ->
total += data.response.total
total
這很好地編譯成
get_total = function(trend, duration) {
var keyword, total, url, _i, _len, _ref;
total = 0;
_ref = trend.search_terms;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
keyword = _ref[_i];
url = "http://otter.topsy.com/search.json?q=" + keyword + "&window=" + duration;
$.ajax({
url: url,
async: false,
success: function(data) {
return total += data.response.total;
}
});
}
return total;
};
有沒有辦法有沒有使用同步JS總的工作。
我一直在試驗$ .when()。then(),但是當請求的大小是動態的時它會引發問題。
如何合計仍然範圍和正確返回。如果你有時間,我希望進入聊天室並與你談談如果你有時間 – austinbv
@austinbv:我創建了一個聊天室:http://chat.stackoverflow.com/rooms/2862/getting-the-total-from -multiple-ajax-requests –
http://chat.stackoverflow.com/rooms/2862/getting-the-total-from-multiple-ajax-requests何時你可以,而ty – austinbv