Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery .when troubleshooting with variable number of arguments
    primarykey
    data
    text
    <p>I'm having an issue with using jQuery.when() to wait for multiple ajax requests to finish before calling another function.</p> <p>Each ajax request will get JSON data, and looks something like this:</p> <pre><code> function loadData(arg){ var ajaxCall = $.ajax( URL // depends on arg ) .error( .... ); return ajaxCall; } </code></pre> <p>When the request is called, the return value (ajaxCall) is added to a list called ajaxRequests.</p> <pre><code> ajaxRequests = []; ajaxREquests.push(loadData(arg)) </code></pre> <p>When all the requests have been made, I'm trying to pass ajaxRequests to $.when in order to wait for all requests to complete.</p> <pre><code> var defer = $.when.apply($, ajaxRequests); defer.done(function(args){ for (var i=0; i&lt;args.length; i++){ inst.loadData($.parseJSON(args[i].responseText)); } inst.draw(); }); </code></pre> <p>inst is an object that loads and draws graphs based on JSON data.</p> <p>The problem is that it doesn't seem to be actually waiting for the requests to finish - args[i] is an object, but responseText is undefined when the code runs. If I save args[i] and access it later from the console, it works.</p> <p>I suspect the problem is related to using .when with an arbitrary number of arguments, as all the examples I've seen on the web give it a pre-defined argument list.</p> <p>I'm not sure if using apply was the right idea or not, but either way it doesn't work properly and behaves erratically (browser-dependent).</p> <p>Any help would be greatly appreciated.</p> <p>Please let me know if more information is required.<br> I'm using jQuery 1.5</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload