1

Possible Duplicate:
Wait until all jquery ajax request are done?
Multiple ajax calls inside a each() function.. then do something once ALL of them are finished?

The function below runs all this code at document ready but I am trying to run other functions when all the data from these completes.

function loadZonesDistrictsStoresData(){
        $.getJSON('/CampaignMgmt/GetZonesByClient', { 'clientid': clientid }, function (zones){
            $.each(zones, function(index, zoneid){
                var li_tag = '<li id="'+ zoneid +'"> <label><input id="'+zoneid+'" data-id="'+zoneid+'" onchange="zones.selectZone(this.value)" type="checkbox" name="zone_'+zoneid+'" value="'+ zoneid +'"><span>' + zoneid +'</span> </label></li>';
                $("ul.zones").append(li_tag);
                $.getJSON('/CampaignMgmt/GetDistrictsByZone', {'clientid': clientid, 'zone': zoneid }, function(data){
                    zone_object[zoneid] = data;
                    $.each(data, function(index, districtid){
                        $.getJSON('/CampaignMgmt/GetStoresByDistrict', {'clientid': clientid, 'district': districtid }, function(stores){
                            districts_object[districtid] = stores;
                        });
                    });
                });
            });
        });
    }
Community
  • 1
  • 1
user1074316
  • 133
  • 1
  • 2
  • 11
  • possible duplicate of [Wait until all jquery ajax request are done?](http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-request-are-done) and [Multiple ajax calls inside a each() function.. then do something once ALL of them are finished?](http://stackoverflow.com/questions/8726046/multiple-ajax-calls-inside-a-each-function-then-do-something-once-all-of-the). – Felix Kling Oct 22 '12 at 19:18
  • probably use `.done` with [jQuery deferred object](http://api.jquery.com/category/deferred-object/) – xkeshav Oct 22 '12 at 19:19
  • That is *a lot* of ajax. Any way to combine the calls on the backend? If not `.done` will most likely do what you need. – Mark Coleman Oct 22 '12 at 19:20
  • you can either us deferred objects as @diEcho mentioned, or a custom queue using jQuery.queue() and jQuery.dequeue() – Derek Oct 22 '12 at 19:24

0 Answers0