如何在一个ajax调用完成后进行其他ajax调用,第二个ajax调用不应该调用.stop函数

时间:2023-01-14 21:17:13

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

实际上我正在调用多个ajax调用,但我希望首先完成一些ajax调用并执行其stop函数中定义的atsk。但是,一旦在stop方法中定义的函数执行了一些ajax调用,那些ajax调用就不应该调用cal.stop方法再来一次

    $('.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

另一个ajax调用此处,但这次不应该调用.ajaxstop

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

假设有两个ajax调用webservice。当一个ajax调用完成依赖于tah的函数ajax调用应该在等待第二个jax调用执行之前执行。例如:一个ajax调用创建图表和其他ajax调用日志用户活动。如果显示chaet的功能依赖于创建图表所以函数在调用日志活动的aax调用之前执行。没有等待ajajx调用用户活动

2 个解决方案

#1


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?

默认情况下,所有请求都是异步发送的。如果需要同步请求,请将此选项设置为false。请注意,同步请求可能会暂时锁定浏览器,在请求处于活动状态时禁用任何操作。另见这个老问题。如何让jQuery执行同步而非异步的Ajax请求?

#2


0  

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

从jQuery 1.8开始,不推荐使用async:false和jqXHR($ .Deferred);您必须使用完整/成功/错误回调。

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

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.

我发现这有点烦人......开发人员应该选择使用async进行阻塞调用:如果平台允许的话,可以使用false - 为什么要限制它?我只是设置一个超时来最小化挂起的影响。

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

尽管如此,我现在在1.8中使用队列,这是一个非阻塞的,并且工作得非常好。 Sebastien Roch创建了一个易于使用的实用程序,允许您排队功能并运行/暂停它们。 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:

在前2个函数中,我将队列对象传递给调用,这是调用的样子:

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.

注意queue.pause();在每个函数的开头,queue.run()在完整语句的末尾继续执行队列。

#1


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?

默认情况下,所有请求都是异步发送的。如果需要同步请求,请将此选项设置为false。请注意,同步请求可能会暂时锁定浏览器,在请求处于活动状态时禁用任何操作。另见这个老问题。如何让jQuery执行同步而非异步的Ajax请求?

#2


0  

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

从jQuery 1.8开始,不推荐使用async:false和jqXHR($ .Deferred);您必须使用完整/成功/错误回调。

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

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.

我发现这有点烦人......开发人员应该选择使用async进行阻塞调用:如果平台允许的话,可以使用false - 为什么要限制它?我只是设置一个超时来最小化挂起的影响。

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

尽管如此,我现在在1.8中使用队列,这是一个非阻塞的,并且工作得非常好。 Sebastien Roch创建了一个易于使用的实用程序,允许您排队功能并运行/暂停它们。 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:

在前2个函数中,我将队列对象传递给调用,这是调用的样子:

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.

注意queue.pause();在每个函数的开头,queue.run()在完整语句的末尾继续执行队列。