1

I have a problem that a function keeps getting called before a loop finishes. How do I prevent the function from being called until after the loop finishes? My loop is outputting HTML tables to the page which is then being acted upon by my sortResultsByDate, but that can't happen until the HTML is rendered. Currently my second function is firing before the first one completes.

ns.executeUserSearch = function() {
    jQuery.each(searchInputArray, function(index, value) {
        ns.getUserSearchResults(searchInputArray, index);
    });

    ns.sortResultsByDate();
};
hallucinations
  • 3,424
  • 2
  • 16
  • 23
kyle_13
  • 1,173
  • 6
  • 25
  • 47

1 Answers1

0

It seems you're calling ns.getUserSearchResults recursively inside the jQuery.each loop. ns.sortResultsByDate() is being executed once for every such call, so you end up executing it multiple times before you complete all your recursive calls.

airbag
  • 75
  • 6