开发者

Hook into the end of an asynchronous javascript call

开发者 https://www.devze.com 2023-04-07 03:22 出处:网络
I\'m calling a javascript function provided by a third-party.This function fires off a few ajax/asynchronous requests before returning, and (impolitely) neither allows me to pass a 开发者_运维问答call

I'm calling a javascript function provided by a third-party. This function fires off a few ajax/asynchronous requests before returning, and (impolitely) neither allows me to pass a 开发者_运维问答callback, nor returns any references to the asynchronous calls it is making.

Is there any way I can trigger something when all of the calls it's making complete? Is there a way I can track 'calls' launched from a javascript function? Forgive my terminology; I'm a javascript beginner.


Is this a library that's loaded from an external source, or do you have your own local copy? In the latter case, you could patch the code to do what you want. Otherwise, I don't really see a way to directly intercept the calls.

If you know exactly what state changes occur for each completed async call, you could set a function to execute on an interval (setInterval(fn, millis)) and check whether all those states have been met. Then, you could fire off some sort of final function to indicate completion.

ie:

var completionHandler = function() {     /* do something awesome */ }

var checkCompletion = function() { 
   if (state1 && state2 && state3) {
        clearInterval(interval); // make sure you clear the interval.
        completionHandler();
   } 
}

var interval = setInterval(checkCompletion, 200);


If your third party script is based on jQuery you could try to modify the ajax behaviour with $.ajaxSetup(), set a context object and bind a ajaxComplete handler to this object. But that might mess up the third party library.

0

精彩评论

暂无评论...
验证码 换一张
取 消