By keshav84


2010-05-31 09:18:08 8 Comments

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

2 comments

@BishopZ 2012-04-28 16:42:21

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.

@Tim Lovell-Smith 2014-07-08 18:52:36

One thing I'm unclear on that's kind of important to me: when does Frame.init() return in your answer? Before, or after all those calls are complete?

@BishopZ 2014-07-08 20:35:05

Frame.init simply tells Frame that when it receives a job, it should run the job. In Frame v2, it was renamed to Frame.start. The alternative use would be if you wanted to add a bunch of things to the Frame que, but didn't want Frame to start running the jobs yet. Frame will simply hold all the jobs until .start() is called.

@jAndy 2010-05-31 09:20:54

Like Obama would say: YES YOU CAN !

jQuery .ajax()

Setting

async = false

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

@Doug 2012-06-28 21:02:13

This functionality is deprecated as of jQuery 1.8 api.jquery.com/jQuery.ajax

@Andrew C 2012-07-03 14:36:03

What's the recommended alternative?

Related Questions

Sponsored Content

14 Answered Questions

33 Answered Questions

[SOLVED] How can I upload files asynchronously?

32 Answered Questions

[SOLVED] How to manage a redirect request after a jQuery Ajax call

36 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

56 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

27 Answered Questions

[SOLVED] How can I refresh a page with jQuery?

42 Answered Questions

[SOLVED] Is there an "exists" function for jQuery?

  • 2008-08-27 19:49:41
  • Jake McGraw
  • 733518 View
  • 2697 Score
  • 42 Answer
  • Tags:   javascript jquery

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

35 Answered Questions

[SOLVED] Add table row in jQuery

17 Answered Questions

[SOLVED] Abort Ajax requests using jQuery

Sponsored Content