开发者

Multiple ajax request state?

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";
                        act开发者_StackOverflow社区ualizacion_completa.style.display = "";
                    }
                }
            }
        }
        else{
            break;
        }       
    }
}

the problem is ready never gets equal to limit.

PS: srry about the spanish variable's names;


You already know the limit is 20, no need to augment it every time with this limit = i;.

Just set

var limit = 20;

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:

console.log('ready = ' + ready);

This way you can actually see at which value ready is stopping, or if it's not augmenting at all.


the value of index inside your onreadystatechange function will not be what you expect.

Suggest replacing the current onreadystatechange with:

(function(ajaxItem, ready) {
    ajaxItem.onreadystatechange = function() {
      if (ajaxItem.readyState==4 && ajaxItem.status == 200) {
        ready++;

        if(ready == limit){
            loading.style.display = "none";
            actualizacion_completa.style.display = "";
        }
    }
})(ajax[index], ready);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜