Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To wait for all handlers to finish before executing some code, you should use jQuery's <code>deferred</code> API. You can do something like this:</p> <pre><code>$.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function(a1, a2){ // only executes after both ajax calls have completed }); </code></pre> <p>Further, jQuery's <a href="http://api.jquery.com/trigger/" rel="noreferrer"><code>trigger</code></a> allows you to pass extra params. Pass in a function which will be the callback function.</p> <p>Your final code should look something like this:</p> <pre><code>$doc.trigger('beforedo', function() { // anything here only executes after the event has been handled }); $doc.on('beforedo',function(e, callback) { //do before addon stuff $.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function(a1, a2){ // at this point, all your AJAX calls have completed // call the callback function callback(); }); } </code></pre> <p>If need be, when you call <code>callback()</code> you can even pass in any result you might need to pass as an argument. Accordingly, change the function signature in your <code>.trigger()</code> call too.</p> <p>Expanding on the comment I left below, you can have a common loader function if you like:</p> <pre><code>$.when(load("page1"), load("page2")).done(function(){ // ... }); function load(file) { // return the $.ajax promise return $.ajax(file); } </code></pre> <p>See:</p> <p><a href="http://api.jquery.com/category/deferred-object/" rel="noreferrer">jQuery deferred</a><br> <a href="http://api.jquery.com/jQuery.when/" rel="noreferrer">jQuery.when()</a><br> <a href="http://api.jquery.com/trigger/" rel="noreferrer">jQuery.trigger</a></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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