如何等待异步功能完成然后继续循环?

时间:2015-04-20 05:06:25

标签: javascript jquery ajax sharepoint asynchronous

你能帮助我吗?我需要等到异步功能在循环中完成,然后继续循环。我想获取附件文件的URL,然后将其插入到html中。



function getProjects(){
	$.ajax({
		    url: "/_api/web/lists/getbytitle('Projects')/items?$orderby=Title asc",
		    method: "GET",
		    headers: {
		        "accept": "application/json;odata=verbose"
		    },
		    success: function (data){
		    	var items = data.d.results;
		    	var result = '';
		    	for (var i = 0; i < items.length; i++) {
		    		result += '<tr>';
			    	result += '<td>'+items[i].Organization+'</td>';
			    	result += '<td>'+items[i].Title+'</td>';
			    	result += '<td>'+items[i].Contact+'</td>';
			    	result += '<td>'+items[i].Description+'</td>';
			    	result += '<td>'+items[i].Manager+'</td>';
			    	result += '<td>'+items[i].Participates+'</td>';	    
			    	getAttachments(items[i].Id).done(function(url){
			    		console.log('1');
			    	});
                    console.log('2');
			    	result += '</tr>';

		        }
		        $('#tableProjects').html(result);	
		    },
		    error: function (err) {
		        alert(JSON.stringify(err));
		    }
		});	
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

有2个选项

a)使用ajax参数async (不建议使用此方法)

   async : false

上述参数将确保您的浏览器等待呼叫完成,然后它将继续进行。从用户的角度来看,上述方法并不好,因为如果你上传文件或请求需要很长时间,那么对于用户来说它会出现浏览器挂起的情况

b)在成功函数中编写完整代码:这是更好的方法,通过你的代码,找到你想要在ajax调用后执行的代码片段,并将其粘贴到成功回调。这种方法看起来有点困难,但一旦使用了这种方法,你就会理解javascript中异步回调的强大功能。

答案 1 :(得分:1)

我希望您可以使用$.when.then,如下所示:

首先在函数内声明一个boolean var

function getProjects(){
var valid=false;
$.when(
$.ajax({
            url: "/_api/web/lists/getbytitle('Projects')/items?$orderby=Title asc",
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose"
            },
            success: function (data){
                    dataObtained=data;
                    valid=true;
                }
                $('#tableProjects').html(result);   
            },
            error: function (err) {
                alert(JSON.stringify(err));
            }
        })).then(function(){
           if(valid)
           {
                var items = dataObtained.d.results;
                var result = '';
                for (var i = 0; i < items.length; i++) {
                    result += '<tr>';
                    result += '<td>'+items[i].Organization+'</td>';
                    result += '<td>'+items[i].Title+'</td>';
                    result += '<td>'+items[i].Contact+'</td>';
                    result += '<td>'+items[i].Description+'</td>';
                    result += '<td>'+items[i].Manager+'</td>';
                    result += '<td>'+items[i].Participates+'</td>';     
                    getAttachments(items[i].Id).done(function(url){
                        console.log('1');
                    });
                    console.log('2');
                    result += '</tr>';
            }
         });    
 }

答案 2 :(得分:0)

我做到了! )))

&#13;
&#13;
function uploader(i,items) {
  if( i < items.length ) {
  	table += '<tr>';
	table += '<td>'+items[i].Organization+'</td>';
	table += '<td>'+items[i].Title+'</td>';
	table += '<td>'+items[i].Contact+'</td>';
	table += '<td>'+items[i].Description+'</td>';
	table += '<td>'+items[i].Manager+'</td>';
	table += '<td>'+items[i].Participates+'</td>';
	getAttachments(items[i].Id).done(function(url){
		table += '<td>'+url+'</td>';
		table += '</tr>';
		uploader(i+1,items);
	});
  }else{
  	$('#tableProjects').html(table);
  }
}

function getProjects(){
	$.ajax({
		    url: "/_api/web/lists/getbytitle('Projects')/items?$orderby=Title asc",
		    method: "GET",
		    headers: {
		        "accept": "application/json;odata=verbose"
		    },
		    success: function (data){
		    	var items = data.d.results;
		    	table="";
		        uploader(0,items);	
		    },
		    error: function (err) {
		        alert(JSON.stringify(err));
		    }
		});	
}
&#13;
&#13;
&#13;

相关问题