/** * @author:caihaibin * @desc:用于解决并发请求后的集中处理,如果回调后的有特殊操作,可以在回传参数里做特殊标记, * @example:{type:'test',result:''} //<==遍历里有test做特殊处理 * @demo: * majax([ 'test.php', 'test.php?r=23', 'test.php?r=2333', * 'test.php?r=233333', 'test.php?test=true' ], function( * responses) { * for ( var index in responses) { * if (responses[index].state == 200) { * console.log(responses[index].result); * } * } * }); */ function majax(requests, recall) { var responses = []; if (!$.isArray(requests)) { recall(responses); } var responseindex = 0; var requestlength = requests.length; if (!requestlength) { recall(responses); } for ( var index in requests) { $.ajax({ url : requests[index], success : function(result, textStatus) { responses[responseindex] = { state : 200, result : result }; responseindex++; if (responseindex == requestlength) { recall(responses); } }, error : function(obj, textStatus) { responses[responseindex] = { state : textStatus }; responseindex++; if (responseindex == requestlength) { recall(responses); } } }); } }
转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827