title: Loop through XML files jQuery
body: <p>I am a bit stuck with the following problem.</p>
  
  <p>I have several XML files tagged by an ID (every XML is id'd by a value). I am now trying to loop through these files and output its contents to HTML.</p>
  
  <p>However it starts the loop before it does the call back</p>
  
  <p>Loop0
  Loop1
  Loop2
  Callback0
  Callback1
  Callback2</p>
  
  <p>I would need
  Loop0
  Callback0
  Loop1
  Callback1</p>
  
  <p>As I need to control the results at some point.</p>
  
  <pre><code>        var allContent=["xmlfile1","xmlfile2","xmlfile3","xmlfile4"];
          var totalSearch = 0;
          var countSearch = 0;
  
          function doSearch() {
  
              var oldContentID = contentID;
  
              for (iSearch=0;iSearch&lt;allContent.length;iSearch++) {
                  totalSearch = totalSearch + countSearch;
                  contentID = allContent[iSearch];
                  defineContent();
  
                  getXML();
  
              }
          } 
  
          function getXML() {
              $.ajax({
                  type: "GET",
                  url: langFile,
                  dataType: "xml",
                  beforeSend: function(){
  
                      $('#results-list').empty();
                      $('#results-list').hide();
                      $('#loading').addClass('loading');
                  },
                  success: function(xml) {
  
                      var totalElements;
                      var intSearch = 0;
                      totalSearch = totalSearch + countSearch;
                      countSearch = 0;
                      var searchText = $('#text').val().toLowerCase();
  
                      totalElements = $(xml).find('news').length;
  
  
                      while (intSearch &lt; totalElements) {
                          oFeed = $(xml).find('news:eq('+intSearch+')');
                          var headline = oFeed.find('headline').text();
                          var newsText = oFeed.find('detail').text();
                          var section = oFeed.find('section').text();
                          var category = oFeed.attr('category');
  
                          var stripEnters = newsText.match(/\r?\n|\r/gi);
                          if (stripEnters != null) {
                              for (var s = 0; s &lt; stripEnters.length ; s++ ){
                                  newsText = newsText.replace(stripEnters[s],'');
                              }
                          }
  
                          var newsText2 = $.htmlClean(newsText, {format:true});
                          var newsText3 = $(newsText2)
                          var newsText4 = $(newsText3).text();
                          var newsText5 = newsText4.replace( /\W/gi, "" );
  
                          if (section.toLowerCase() == "news" || section.toLowerCase() == "featured") {
                              if (headline.toLowerCase().indexOf(searchText) &gt;= 0) {
                                  $('&lt;dt&gt;&lt;/dt&gt;').html(headline).appendTo('#results-list');
                                  $('&lt;dd&gt;&lt;/dd&gt;').html(newsText).appendTo('#results-list');
                                  countSearch++;
                              }//end if
                              else if (newsText5.toLowerCase().indexOf(searchText) &gt;= 0) {
                                  $('&lt;dt&gt;&lt;/dt&gt;').html(headline).appendTo('#results-list');
                                  $('&lt;dd&gt;&lt;/dd&gt;').html(newsText).appendTo('#results-list');
                                  countSearch++;
                              }
                          }
                          intSearch++;
                      }           
  
                  }   
              }); 
          }
  </code></pre>
  
  <p>At the end of the call backs I need to run the following, however it now executes this function before it finishes all call backs.</p>
  
  <pre><code>        function displayResults() {
              if (totalSearch == 0)
              {
                  alert("No results found");
                  $('#loading').removeClass('loading');
                  $('#main').fadeIn(1000);
              }
              else {
                  dynamicFaq();
                  $('&lt;p&gt;&lt;/p&gt;').html(totalSearch + ' Results found').prependTo('#results-list');
                  $('#results-list').fadeIn(1000);
                  $('#loading').removeClass('loading');
              }   
          }
  </code></pre>