多个ajax请求状态?
我正在编写一个需要执行一些并行 ajax 请求的脚本。我正在使用 ajax 数组,但我想在所有请求完成时执行一些代码(使一些标签可见)。这是代码希望你能帮助我。
function Update_All(){
var loading = document.getElementById("loading_image");
var actualizacion_completa = document.getElementById("actualizacion_completa");
loading.style.display = "";
actualizacion_completa.style.display = "none";
var i=1;
var ajax= new Array(20);//objetoAjax();
var limit = 1;
var ready = 1;
for (i=1;i<=20;i++)
{
var index = i-1;
ajax[index] = objetoAjax();
var voatencion = document.getElementById("idatencionf"+i);
var vohoras = document.getElementById("vhorasf"+i);
var vtarifa = document.getElementById("tarifaclientef"+i);
var voexonerado = document.getElementById("vexoneradof"+i);
var voaprobado = document.getElementById("vaprobadof"+i);
var campo = document.getElementById("f"+i+"c1");
if(typeof(campo) != 'undefined' && campo != null){
var valor = 1;
if(campo.innerHTML=="Si") valor = 2;
ajax[index].open("GET","update.php?atencion="+voatencion+"&opc="+5+"&valor="+valor+"&exonerado="+voexonerado.value+"&tarifa="+vtarifa.value+"&horas="+vohoras.value);
ajax[index].send(null);
limit = i;
ajax[index].onreadystatechange=function() {
if (ajax[index].readyState==4 && ajax[index].status == 200) {
ready++;
if(ready == limit){
loading.style.display = "none";
actualizacion_completa.style.display = "";
}
}
}
}
else{
break;
}
}
}
问题已经准备好,永远不会达到极限。
PS:抱歉西班牙变量的名称;
I'm writing a script that need that executes some paralleled ajax requests. I'm using an ajax array but i want to execute some code(make some tags visible) when all the requests are complete. here's the code hope u can help me.
function Update_All(){
var loading = document.getElementById("loading_image");
var actualizacion_completa = document.getElementById("actualizacion_completa");
loading.style.display = "";
actualizacion_completa.style.display = "none";
var i=1;
var ajax= new Array(20);//objetoAjax();
var limit = 1;
var ready = 1;
for (i=1;i<=20;i++)
{
var index = i-1;
ajax[index] = objetoAjax();
var voatencion = document.getElementById("idatencionf"+i);
var vohoras = document.getElementById("vhorasf"+i);
var vtarifa = document.getElementById("tarifaclientef"+i);
var voexonerado = document.getElementById("vexoneradof"+i);
var voaprobado = document.getElementById("vaprobadof"+i);
var campo = document.getElementById("f"+i+"c1");
if(typeof(campo) != 'undefined' && campo != null){
var valor = 1;
if(campo.innerHTML=="Si") valor = 2;
ajax[index].open("GET","update.php?atencion="+voatencion+"&opc="+5+"&valor="+valor+"&exonerado="+voexonerado.value+"&tarifa="+vtarifa.value+"&horas="+vohoras.value);
ajax[index].send(null);
limit = i;
ajax[index].onreadystatechange=function() {
if (ajax[index].readyState==4 && ajax[index].status == 200) {
ready++;
if(ready == limit){
loading.style.display = "none";
actualizacion_completa.style.display = "";
}
}
}
}
else{
break;
}
}
}
the problem is ready never gets equal to limit.
PS: srry about the spanish variable's names;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经知道限制为 20,无需每次都使用
limit = i;
来增加它。设置即可
只需在脚本的开头
。由于数组以 0 开头,您实际上可能需要为限制少设置一个值(本例中为 19)。
此外,每次完成 AJAX 请求时都添加这一行:
这样您就可以实际看到在哪个位置价值就绪正在停止,或者根本没有增加。
You already know the limit is 20, no need to augment it every time with this
limit = i;
.Just set
At the beginning of your script.
It may be that you actually need to set one value less for the limit (19 in this case) due to the array starting with 0.
Also, add this line every time you complete an AJAX request:
This way you can actually see at which value ready is stopping, or if it's not augmenting at all.
onreadystatechange 函数中的 index 值不会是您所期望的。
建议将当前的 onreadystatechange 替换为:
the value of index inside your onreadystatechange function will not be what you expect.
Suggest replacing the current onreadystatechange with: