Synchronous calls with jquery

后端 未结 2 1374
渐次进展
渐次进展 2020-11-30 11:48

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

2条回答
  •  生来不讨喜
    2020-11-30 12:07

    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.

提交回复
热议问题