我正在尝试为我的网站设置ajax进度条。有时,多个Ajax调用链接在一起,这是我的代码jquery ajax进度条
var xhrs = [];
function ajaxCall(){
xhr = $.ajax({
...other ajax code...
success: function(data){
anotherAjaxCall(data);
}
});
xhr.onreadystatechange = reportStatus;
xhrs.push(xhr);
}
function anotherAjaxCall(data){
xhr = $.ajax({
...other ajax code...
});
xhr.onreadystatechange = reportStatus;
xhrs.push(xhr);
}
...lots more functions that make ajax calls....
function reportStatus(){
var overallPercent = 0;
for(i = 0; i < xhrs.length; i++){
overallPercent += (xhrs[i].readyState * 20);
}
var percent = overallPercent/xhrs.length;
alert(percent + " = " + overallPercent + "/" + xhrs.length);
//update progress bar
updateProgressPercentage(percent);
}
从reportStatus功能一切发生的警报是第一个Ajax调用得到4 readyState的,它调用不运行AJAX的第二个功能打电话给它。有没有人有任何想法为什么第二个函数运行,但内部Ajax调用没有。提前致谢。
添
我假设'anotherAjaxCall'实际上并没有定义为'xhr = $ .ajax({});'? – mellamokb 2011-03-03 04:34:43
是的,我应该让这更明确的抱歉。 – 2011-03-03 04:41:06
为什么你想使用ajax?以及您如何计划持续获得响应并使用它? – 2011-03-03 04:49:53