여러 Ajax 요청의 반환에서 총을 얻으려고하고 있습니다. 지금은 훨씬 더 나은 솔루션이라고 생각하므로 지금 바로 동기화를 사용합니다.여러 Ajax 요청에서 총 가져 오기
여기에 동기 JS를 사용하지 않고 전체 작업을 할 수있는 방법이 있나요
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;
};
에 잘 컴파일 내 커피
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
입니다.
나는 $ .when(). then()을 실험했지만 요청의 크기가 동적 일 때 문제가 발생했습니다.
총계는 여전히 범위가 적절하고 올바르게 반환됩니다. 나는 희망을 갖고 채팅을하고 그것에 대해 당신이 시간이 있다면 – austinbv
@austinbv : 나는 대화방을 만들었습니다 : http://chat.stackoverflow.com/rooms/2862/getting-the-total-from 언제든지 할 수있을 때 복수형 아약스 요청 –
http://chat.stackoverflow.com/rooms/2862/getting-the-total-from-multiple-ajax-requests 그리고 – austinbv