2017-05-04 55 views
0

我在循環中使用$ .get(),我需要腳本在繼續循環之前等待結果。

下面的代碼片段:

for(i=0;i<4;i++) { 
     //some code 
     $.get(url,function(data1){ 
     //some code 
    }); 
     //some code 
    } 

回答

0

使用異步:假

$.ajax({ 
    url:'your url', 
    method:'GET' 
    async:false, 
    success:function(result){ 
     //some code 
    } 
}); 
0

如果你想繼續使用速記$.get你可以設置AJAX使您的請求之前是同步的像jQuery.ajaxSetup({async:false});並且可能在代碼jQuery.ajaxSetup({async:true});後重新設置爲true。