sample code:
var isExecutionOver = false,
myFunction = function() {
// does some asynchronous stuff and sets isExecutionOver to true
// when the asynchronous job is over.
};
myFunction();
// Kill myFunction if it takes more than 3 seconds
setTimeout(function() {
if(!isExecutionOver) {
// How do I kill myFunction?
}
}, 3*1000);
In the above snippet I am trying to kill (or in other words, stop execution of) myFunction if it is not able to do its job in given time (3 seconds in this case).
PS: Kindly assume that I do not have control over myFunction definition. The only place where I can work is inside setTimeout.
.abort()it. To be clear,myFunctionwill have exited long before the async stuff is done. What you want to cancel is whatever pending async operation that was started bymyFunction, notmyFunctionitself.