4

actually i 'm calling multiple ajax calls but i want some ajax calls completes first and exectute the atsk defined in its stop function.but some ajax call should be made once function defined in stop method executes and those ajax call should not cal.stop method once again

    $('.badgeContainer').ajaxStop(function() 
    {

          alert("ajax rqst completed");
    });

 $.ajax({

 type: "POST",

           url: "WebServices/abc.asmx/dosomething",
        data: "{  }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(serviceReply) {
                          var serviceReplyArray = serviceReply.d.split("#");

                    },
                    error: function(xhr, errDesc, exception) {
                                                                    }
                });

another ajax call here but should not call .ajaxstop this time

i'm putting my question in this way:

suppose there are two ajax call to webservice. when one ajax call completes the function dependent on taht ajax call should get executed before waiting for second jax call execution.for eaxample: one ajax call create chart and other ajax call log user activities.if a functon to display chaet is dependent on create chart so that function get executed before calling aax call for log activities.no waiting for ajajx call for user activities

user1403235
  • 49
  • 1
  • 3
  • where's the code for the ajax call? – Rune FS Jun 14 '12 at 05:59
  • not really sure what you exactly mean but I think you want a callback function to fire the second call for when the first ajax call completes – hackartist Jun 14 '12 at 06:01
  • define global variable . set it when ajax call completes . before starting next ajax call , check global variable and based on your condition make/skip ajax call. – Priyank Patel Jun 14 '12 at 06:07
  • actually i want to make another ajax call when first ajax call gets completed and at that time stop method should be automatically called and executed. but when i make second ajax call it should not trigger stop method. – user1403235 Jun 14 '12 at 06:08

2 Answers2

4

I think you want to set:

 async: false

By default, all requests are sent asynchronously . If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
See also this old question.
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

Community
  • 1
  • 1
4b0
  • 21,981
  • 30
  • 95
  • 142
  • ajaxstop method will get auto triggered when all jaax request gets completed but i want it should get triggered when some of ajax requests are completed and some are left. those left ajax call should not trigger ajaxstop method.and i dun want synchronous ajax call..i just want the function defined in ajax method should get executed first before making other ajax call. what happening with my code is taht it executes everything when all ajax reqests getting completed – user1403235 Jun 14 '12 at 06:17
  • can i bind one ajax call with one particular selector and other ajax call with other selector – user1403235 Jun 14 '12 at 06:57
  • I think it is not possible :) – 4b0 Jun 14 '12 at 07:10
0

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the complete/success/error callbacks.

http://api.jquery.com/jQuery.ajax/ (async section)

I found this a bit annoying... developers should be given the choice to make a blocking call with async: false if its something the platform allows - why restrict it? I'd just set a timeout to minimize the impact of a hang.

Nonetheless, I'm using a queue now in 1.8, which is non-blocking, and works quite nicely. Sebastien Roch created a easy to use utility that allows you to queue up functions and run/pause them. https://github.com/sebastien-roch/Jquery-Async-queue

    queue = new $.AsyncQueue();
    queue.add(function (queue) { ajaxCall1(queue); });
    queue.add(function (queue) { ajaxCall2(queue); });
    queue.add(function() { ajaxCall3() });
    queue.run();

In the first 2 functions I pass the queue object into the calls, here's what the calls would look like:

function ajaxCall1(queue) {
    queue.pause();
    $.ajax({
       // your parameters ....
       complete: function() {
         // your completing steps
         queue.run();
       }
    });
}

// similar for ajaxCall2

Notice the queue.pause(); at the beginning of each function, and queue.run() to continue queue execution at the end of your complete statement.

Community
  • 1
  • 1
sonjz
  • 4,870
  • 3
  • 42
  • 60