Just for the sake of correctness, this can be done in a clean asynchronous way with jQuery Deferred . These objects represent a task that will be completed in the future, and event handlers can be tied to completion. You can invoke the "Snooze manually" command.
These objects are what jQuery works behind the scenes for most async tasks that have callbacks , and you really can wait for them to complete.
You need to change your code a bit.
In the click event handler for #printpng you need to create deferred objects for each task that you want to complete.
Let's say all three click events that you trigger manually must wait, so we create 3 snoozes.
$('#printpng').click(function () { var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); .......... other code not yet included });
Now we have 3 objects that represent the tasks. If you call .resolve() , it means the task is complete. We want these tasks to be performed after executing the #tool_export click event handler.
Say this #tool_export has a click event handler, and somehow we can pass a pending object to it.
$('#tool_export').click(function (e, deferred) { $.ajax({ url: 'your_url', success: function(result){
As you can see, it calls the AJAX call and calls deferred.resolve() if the call was successful and deferred.reject() if something went wrong. Very simple.
Now the problem is this: how do I pass this pending parameter to click event handlers?
You wrote:
$('#tool_docprops').click();
This is a shorthand for:
$('#tool_docprops').trigger('click');
To pass def1 as a parameter, you can simply write:
$('#tool_docprops').trigger('click', def1);
So your event handler changes to:
$('#printpng').click(function () { var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); $('#tool_docprops').trigger('click', def1); $('#tool_docprops_save').trigger('click', def2); $('#tool_export').trigger('click', def3); ..... something is still missing from here });
You can pass as many parameters as you want.
The last thing to do is to defer this delay until completion. There is a very cool helper method .when() that expects any number of pending requests to be resolved. Since it also creates deferred, you can call deferred.done() to get a callback. So, to wait for all 3 pending before, you could write:
$.when(def1, def2, def3).done(function() { window.location.href = "<?php echo $base_url ?>/sign/print"; });
So your last click event handler for #printpng will look like this:
$('#printpng').click(function () { var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); $('#tool_docprops').trigger('click', def1); $('#tool_docprops_save').trigger('click', def2); $('#tool_export').trigger('click', def3); $.when(def1, def2, def3).done(function() { window.location.href = "<?php echo $base_url ?>/sign/print"; }); });
I made a very simple example to show this. There is no ajax call there just a timeout, but you can get this idea.
If you press the Start button, you will need to wait 4 seconds to complete:
http://jsfiddle.net/3ZDEe/2/