执行功能后Ajax调用完成 [英] Execute function after Ajax call is complete

查看:131
本文介绍了执行功能后Ajax调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Ajax,我试图同时使用一个for循环使用Ajax。 Ajax调用后,我正在使用的Ajax调用创建的变量的函数。该函数仅执行两次。我认为Ajax调用可能没有足够的时间来进行呼叫的循环重新开始之前。有没有一种方法,以确认运行功能printWithAjax()之前Ajax调用? 我不希望printWithAjax()函数来执行,直到Ajax调用完成。任何帮助将大大AP preciated。

  VAR ID;
VAR vname;
功能AjaxCall的(){
对于(VAR Q = 1; Q< = 10,Q ++){
 $阿贾克斯({
         网址:api.php,
         数据:'ID1 ='+ Q +'',
         数据类型:JSON,
         异步:假的,
         成功:函数(数据)
         {
            的id =数据[0];
            vname =数据[1];
         }
      });

       printWithAjax();

 } for语句//结束
的Ajax调用函数} //结束
 

解决方案

试试这个code:

  VAR ID;
VAR vname;
功能AjaxCall的(){
对于(VAR Q = 1; Q< = 10,Q ++){
 $阿贾克斯({
     网址:api.php,
     数据:'ID1 ='+ Q +'',
     数据类型:JSON,
     异步:假的,
     成功:函数(数据)
     {
        的id =数据[0];
        vname =数据[1];
     },
    完成:功能(数据){
      printWithAjax();
     }
    });

  } for语句//结束
  的Ajax调用函数} //结束
 

完成功能后,才AJAX的成功执行。因此,尝试调用printWithAjax()上的完成。这应该为你工作。

I am new to Ajax and I am attempting to use Ajax while using a for loop. After the Ajax call I am running a function that uses the variables created in the Ajax call. 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. Is there a way to confirm the Ajax call before running the function printWithAjax()? I do not want the printWithAjax() function to execute until the Ajax call is complete. 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. So try to call the printWithAjax() on "complete". This should work for you.

这篇关于执行功能后Ajax调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆