Synchronous calls with jquery

会有一股神秘感。 提交于 2019-12-17 07:37:30

问题


Can I make use of jQuery AJAX API and make a synchronous calls?


回答1:


Like Obama would say: YES YOU CAN !

jQuery .ajax()

Setting

async = false

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




回答2:


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.



来源:https://stackoverflow.com/questions/2942544/synchronous-calls-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!