ajax多次请求的一个效果思路
首先页面加载时候显示遮罩层
jQuery(function() { show_dialog(); //tianxie(); });
定义一个全局数组,用于存放问题id
var qar = [];
循环问题id,并赋值到qar数组中
for (quid_index = 0; quid_index < results.totalProperty; quid_index++) { questions[quid_index] = []; questions[quid_index][0] = results.content[quid_index].quid; //问题编号 questions[quid_index][1] = results.content[quid_index].total; //问题类型 qar[quid_index] = results.content[quid_index].quid; }
最后在ajax 请求成功后的回调函数里,做递减操作,判断最后回调是否为0(这里是5,程序还没完全写完,只能判断到这里。),最后遮罩层隐藏。
function optionsDataAjax(param, field) { var options = jQuery.ajax({ async: true, type: "get", url: "${homeMoudle}/home/quickreport/optionsDataRes.htm", data: param + field, success: function(data,s,k) { //alert("option======="+data); var txt = eval('(' + data + ')'); var json = eval(txt.content); for (var i = 0; i < json.length; i++) { var quid = json[i].quid; var total = json[i].total; jQuery("#q" + quid).html('(' + total.toFixed(1) + '%)'); } qar.splice(0,1); //console.log("optionsDataAjax==="+qar.length); if(qar.length==5){ tianxie(); } } }); } function fuxuanAjax(param, field) { jQuery.ajax({ async: true, type: "get", url: "${homeMoudle}/home/quickreport/getFuxuan.htm", data: param + field, success: function(data) { //alert("option======="+data); var txt = eval('(' + data + ')'); var json = eval(txt.content); for (var i = 0; i < json.length; i++) { var quid = json[i].quid; var total = json[i].total; jQuery("#q" + quid).html('(' + total.toFixed(1) + '%)');; } qar.splice(0,1); if(qar.length==5){ tianxie(); } } }); } function numberDataAjax(param, field) { jQuery.ajax({ async: true, type: "get", url: "${homeMoudle}/home/quickreport/numberDataRes.htm", data: param + field, success: function(data) { //alert("number======="+data); var txt = eval('(' + data + ')'); if (txt.quid == 159 || txt.quid == 445 || txt.quid == 533 || txt.quid == 534) { var size = jQuery("#companysize").html(); var val = txt.total / size; jQuery("#q" + txt.quid).html(val.toFixed(2)); } else { jQuery("#q" + txt.quid).html(txt.total.toFixed(2)); } qar.splice(0,1); if(qar.length==5){ tianxie(); } } }); } function tianxie() { hide_dialog(); }