简体   繁体   English

延迟后续ajax调用,直到第一个ajax调用完成

[英]Delay subsequent ajax call until first ajax call is complete

I am having some trouble with the timing of javascript events. 我在javascript事件的时机方面遇到了一些麻烦。 The problem I am having is that one part of the code seems to be executing before another part of the code completes. 我遇到的问题是代码的一部分似乎在代码的另一部分完成之前执行。 I need to ensure that the first code finishes before the latter code begins. 我需要确保第一个代码在后一个代码开始之前完成。 Here is the initial code: 这是初始代码:

function(){     
    myLoop();  //this needs to complete before the call to myMethod below 
    $.ajax({
    url: sURL + "myController/myMethod",
    success: function() {       
    $.msg("My Success Message",{live:10000});
    error: function(){
    $.msg("My Error Message",{live:10000});
});
}

And here is the code that loops and inserts records into a db: 以下是循环并将记录插入db的代码:

function myLoop(){
$('input[name=c_maybe].c_box').each(function(){
if( $(this).prop('checked') ){ 
    var rn = $(this).prop('value'); 
    $.ajax({
        url: sURL + 'myController/myInsert',
        type:"POST",
        dataType: 'text',
        data: {'rn': rn},
        success: function(data) {
            //not sure what to do on success.
        }
    });
} 
}); 
} 

The problem that seems to be happening is that the call to myController\\myMethod is happening before myLoop completes inserting all the records into the database. 似乎正在发生的问题是在myLoop完成将所有记录插入数据库之前调用myController\\myMethod

Can someone suggest a way for me to redesign this code so that I can ensure that myController\\myMethod is not called until myLoop has completely finished? 有人可以建议我重新设计这个代码的方法,以便我可以确保myLoop完成后才调用myController\\myMethod吗?

Thanks. 谢谢。

You can use the $.when function that has been added to jQuery. 您可以使用已添加到jQuery的$ .when函数。

It goes something like this: 它是这样的:

   $.when(ajaxFunction1(), ajaxFunction1()).done(function(response1, response2){
    // when the function calls are done this code here will be executed -> the response will be passed as parameters corresponding to the functions -> response1, response2
   });

Or you can try to use "beforeSend" within the ajax function: 或者您可以尝试在ajax函数中使用“beforeSend”:

$.ajax({
   beforeSend: function(){    
     alert("doing stuff before the ajax call ...");    
   },
   success: function(){    
    alert("Whoa!");    
   }
 });
function myLoop() {
   var jqxhrs = [];
   if( $(this).prop('checked') ){ 
       var rn = $(this).prop('value'); 
       jqxhrs.push($.ajax({...
   }
   return jqxhrs;
}

function () {
   $.when.apply(undefined, myLoop()).done(function () {
      $.ajax({
         url: sURL + "myController/myMethod",
         ...
   });
}

$.when.apply is used to call $.when on the array of ajax requests, so .done is not called until they are all complete. $.when.apply用于调用$.when Ajax请求的阵列上,因此.done不叫,直到他们完成了。

You can make the ajax call synchronous. 您可以使ajax调用同步。 That way, the execution will be blocked till ajax call returns: 这样,执行将被阻止,直到ajax调用返回:

$.ajax({
    url: sURL + 'myController/myInsert',
    type:"POST",
    dataType: 'text',
    data: {'rn': rn},
    async: false,
    success: function(data) {
        //not sure what to do on success.
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM