开发者

Synchronous calls with jquery

开发者 https://www.devze.com 2023-01-01 09:29 出处:网络
Can I make use of jQuery AJAX API and make 开发者_JAVA技巧a synchronous calls?Like Obama would say:YES YOU CAN !

Can I make use of jQuery AJAX API and make 开发者_JAVA技巧a synchronous calls?


Like Obama would say: YES YOU CAN !

jQuery .ajax()

Setting

async = false

within the .ajax() handler will do the trick.


While jQuery can make sync AJAX calls by setting the synch:false property, this causes the browser to hang until the AJAX completes. Using a flow control library like Frame.js enables you to make synchronous calls without tying up the browser:

$.each(ajaxObjects, function(i, ajaxCall){
    Frame(function(next)){ // declare the callback next here

        ajaxCall.complete = function(data){
            // do something with the data
            next(); // go to the next ajax call
        }
        $.ajax(ajaxCall);

    });
}
Frame.init();

This series of AJAX calls will be made in order, each waiting for the previous to complete, without making the browser hang. Also has the added benefit that the data returns from the ajax calls in a predictable order, as opposed to asynchronous calls which return in a random order.

0

精彩评论

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

关注公众号