简体   繁体   English

Ajax调用完成后执行函数

[英]Execute function after Ajax call is complete

I am new to Ajax and I am attempting to use Ajax while using a for loop. 我是Ajax的新手,我在尝试使用Ajax时使用for循环。 After the Ajax call I am running a function that uses the variables created in the Ajax call. 在Ajax调用之后,我正在运行一个使用Ajax调用中创建的变量的函数。 The function only executes two times. 该功能仅执行两次。 I think that the Ajax call may not have enough time to make the call before the loop starts over. 我认为Ajax调用可能没有足够的时间在循环重启之前进行调用。 Is there a way to confirm the Ajax call before running the function printWithAjax()? 有没有办法在运行函数printWithAjax()之前确认Ajax调用? I do not want the printWithAjax() function to execute until the Ajax call is complete. 我不想在Ajax调用完成之前执行printWithAjax()函数。 Any help will be greatly appreciated. 任何帮助将不胜感激。

var id;
var vname;
function ajaxCall(){
for(var q = 1; q<=10; q++){
 $.ajax({                                            
         url: 'api.php',                        
         data: 'id1='+q+'',                                                         
         dataType: 'json',
         async:false,                    
         success: function(data)          
         {   
            id = data[0];              
            vname = data[1];
         }
      });

       printWithAjax(); 

 }//end of the for statement
}//end of ajax call function

Try this code: 试试这段代码:

var id;
var vname;
function ajaxCall(){
for(var q = 1; q<=10; q++){
 $.ajax({                                            
     url: 'api.php',                        
     data: 'id1='+q+'',                                                         
     dataType: 'json',
     async:false,                    
     success: function(data)          
     {   
        id = data[0];              
        vname = data[1];
     },
    complete: function (data) {
      printWithAjax(); 
     }
    });

  }//end of the for statement
  }//end of ajax call function

The "complete" function executes only after the "success" of ajax. “完成”功能仅在ajax“成功”之后执行。 So try to call the printWithAjax() on "complete". 因此,尝试在“完成”上调用printWithAjax()。 This should work for you. 这应该适合你。

You can use .ajaxStop() or .ajaxComplete() 您可以使用.ajaxStop().ajaxComplete()

.ajaxComplete() fires after completion of each AJAX request on your page. 完成页面上的每个AJAX请求后会触发.ajaxComplete()

$( document ).ajaxComplete(function() {
  yourFunction();
});

.ajaxStop() fires after completion of all AJAX requests on your page. 完成页面上的所有AJAX请求后,将触发.ajaxStop()

$( document ).ajaxStop(function() {
  yourFunction();
});

Add .done() to your function 将.done()添加到您的函数中

var id;
var vname;
function ajaxCall(){
for(var q = 1; q<=10; q++){
 $.ajax({                                            
         url: 'api.php',                        
         data: 'id1='+q+'',                                                         
         dataType: 'json',
         async:false,                    
         success: function(data)          
         {   
            id = data[0];              
            vname = data[1];
         }
      }).done(function(){
           printWithAjax(); 
      });



 }//end of the for statement
}//end of ajax call function

You should set async = false in head. 你应该在头部设置async = false。 Use post/get instead of ajax. 使用post / get而不是ajax。

jQuery.ajaxSetup({ async: false }); jQuery.ajaxSetup({async:false});

    $.post({
        url: 'api.php',
        data: 'id1=' + q + '',
        dataType: 'json',
        success: function (data) {

            id = data[0];
            vname = data[1];

        }
    });

try 尝试

var id;
var vname;
function ajaxCall(){
for(var q = 1; q<=10; q++){
 $.ajax({  

 url: 'api.php',                        
 data: 'id1='+q+'',                                                         
 dataType: 'json',
 success: function(data)          
  {

    id = data[0];              
   vname = data[1];
   printWithAjax();
}
      });



}//end of the for statement
  }//end of ajax call function

Append .done() to your ajax request. .done()附加到您的ajax请求中。

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() { //use this
  alert("DONE!");
});

See the JQuery Doc for .done() 查看.done()JQuery Doc .done()

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

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