2014-09-04 95 views
3

在循環和執行下一個調用之前,我將如何等待以前的ajax調用完成?目前代碼一直循環並一次執行所有ajax請求!在FOR循環中等待以前的AJAX調用

<script> 
var busy; 

function check(mailpass, proxy){ 
    var post_data = {}; 
    var post_json = ""; 

    post_data['mailpass'] = mailpass; 
    post_data['proxy'] = '108.36.248.67:17786'; 
    post_json = JSON.stringify(post_data); 

    jQuery.ajax({ 
     url: '/postdata' , 
     type: "POST", 
     data: {params: post_json}, 
     success: function(data){ 
      var obj = JSON.parse(data); 

      if(obj.error == 0){ 
       // 
       $("#acc-live").append(obj.msg + "<br/>"); 
      } else if(obj.error == 1){ 
       // 
       $("#socks-die").append(obj.msg+ "<br/>"); 
      } else if(obj.error == 2){ 
       // 
       $("#acc-die").append(obj.msg+ "<br/>"); 
      } 
     } 
    }); 

} 
$(document).ready(function(){ 
    $("#submit").click(function(){ 
     var lines = $("#lines").val().split('\n'); 


     for(var i = 0;i < lines.length;i++){ 
      check(lines[i], '123'); 
     } 

    }); 

}); 
</script> 

回答

3

你可以添加計數器(CURRENTINDEX)和重有點組織代碼。

var busy; 
var lines; 
var currentIndex = 0; 

function checkNext(){ 

    if(currentIndex >= lines.length){ 
     console.log('all done'); 
     return; 
    } 

    var mailpass = lines[currentIndex]; 
    var proxy = '123'; 

    var post_data = {}; 
    var post_json = ""; 

    post_data['mailpass'] = mailpass; 
    post_data['proxy'] = '108.36.248.67:17786'; 
    post_json = JSON.stringify(post_data); 

    jQuery.ajax({ 
     url: '/postdata' , 
     type: "POST", 
     data: {params: post_json}, 
     success: function(data){ 
      var obj = JSON.parse(data); 

      if(obj.error == 0){ 
       // 
       $("#acc-live").append(obj.msg + "<br/>"); 
      } else if(obj.error == 1){ 
       // 
       $("#socks-die").append(obj.msg+ "<br/>"); 
      } else if(obj.error == 2){ 
       // 
       $("#acc-die").append(obj.msg+ "<br/>"); 
      } 

      currentIndex++; //Increase the counter 
      checkNext(); 
     } 
    }); 

} 
$(document).ready(function(){ 
    $("#submit").click(function(){ 
     lines = $("#lines").val().split('\n');   
     checkNext(); 
    }); 
}); 
0

只要有它,這樣你的函數一旦AJAX請求已成功完成check返回一個回調函數,這樣一旦與AJAX請求完成它只會再次啓動循環。

function check(mailpass, proxy, callback) { //Callback function passed as a parameter 

    ... 

    success: function(data){ 

     ... 

     callback(); //Invokes the function to start the loop again... 
    } 
} 

然後當你打電話給你重複它的功能...

i = 0; 
var lines = $("#lines").val().split('\n'); 

var loop = function checkLines() 
    i++ 
    if (i < lines.length) { 
     check(lines[i], '123', function() { 
      loop(); //Calls the function to loop again, checking to see if `i` is less than `lines`... 
     }); 
    } 
} 
0

如果你只是想等待完成Ajax調用爲什麼你不能只讓async : false? (雖然我不建議這樣

jQuery.ajax({ 
    url: '/postdata' , 
    type: "POST", 
    async : false, // here 
    data: {params: post_json}, 
    success: function(data){ 
     var obj = JSON.parse(data); 

     if(obj.error == 0){ 
      // 
      $("#acc-live").append(obj.msg + "<br/>"); 
     } else if(obj.error == 1){ 
      // 
      $("#socks-die").append(obj.msg+ "<br/>"); 
     } else if(obj.error == 2){ 
      // 
      $("#acc-die").append(obj.msg+ "<br/>"); 
     } 
    } 
}); 

,將使循環等待,直到方法完成其執行