Is there a way to fire a function when another function has completed in a manner that is similar to DOM events?
e.g. (I know it's not possible but...)
$('body').on('myFunction', function(){
alert('My function was called.');
})
Is there a way to fire a function when another function has completed in a manner that is similar to DOM events?
e.g. (I know it's not possible but...)
$('body').on('myFunction', function(){
alert('My function was called.');
})
No, there is absolute no way to do that unless you can replace the function (i.e. have access to its scope).
If you do have access to the scope it becomes very easy though and does not need any jQuery at all:
var origFunc = theFunction;
theFunction = function() {
var args = Array.prototype.slice.call(arguments);
// run before the call
var rv = origFunc.apply(this, args);
// run after the call
return rv;
};
See MDN for information about the arguments object containing the arguments passed to the function. Using slice to convert it to a plain array is unfortunately necessary since browsers such as IE9 fail to follow the ES5 standard properly which requires apply to accept any array-like object.
Not sure if this is completely what you want. But I think you should give a try to jquery Deferred objects. Have a look at the jquery documentation here - Jquery Deferred Object.
A brief overview -
I have compiled this little fiddle as a demo. - FIDDLE.
Hope this helps.