简体   繁体   English

Ajax循环内的Ajax

[英]Ajax inside ajax in loop

I have the code (structure): 我有代码(结构):

for (var x = 0; x < array1.length; x++){
(function(x) {
    $.ajax({
    url : "http://someurl1.ru/"+array1[x]+"/",
    async: false,
    success : function(someresult1){
        array2.length = 0;
        array3.length = 0;
        var br = someresult1.split('<br>');
        for (var b = 0; b < br.length-1; b++){
            var space = br[b].split(" ");
            array2.push(space[0]);
            array3.push(space[1]);
        }
        for (var v = 0; v < array2.length; v++){
        (function(v) {
            $.ajax({
            url : "http://someurl2.ru/"+array2[v]+"_"+array3[v]+"/",
            async: false,
            success : function(someresult2){
                if(JSON.stringify(someresult2).search(some_array[x]) != -1){
                $.ajax({
                url : "http://someurl3.ru/"+array2[v]+"/"+array3[v]+"/"+some_another_array[x]+"",
                async: false,
                success : function(someresult3){
                    array4.push(someresult3);
                }
                });
                }
            }
            });
            })(v);
        }
    }
    });
})(x);
}

I need to activate async in my request because it freezes my page and slowing down work of the program. 我需要在请求中激活异步,因为它会冻结我的页面并减慢程序的工作。 There some explanation about program work: 关于程序工作,有一些解释:

1. Take first element of array1.
2. Creating link and sending request.
3. Taking result and doing some stuff with it.
4. Creating link and sending request.
5. Taking result and doing some stuff with it.
6. Creating link and sending request.
7. Taking result.
8. AND ONLY NOW we take second element of array1 and doing the same.

I need of synchronuous/continuous ajax requests (with "wait" result) in loop. 我需要循环中的同步/连续ajax请求(带有“ wait”结果)。

I just restructured the way you request URLs and the kind of callback. 我只是重新构造了您请求URL的方式和回调的类型。 Alert me if something isn't right. 如果出现问题,请通知我。 Although, I didn't change the program actions and style. 虽然,我没有更改程序的动作和样式。 I'm really lazy to make that better. 我真的很懒,要把它变得更好。

Instead of loops I made a function to go consuming each array1 item, for example, and the same with the array2 . 例如,我没有使用循环,而是使用一个函数来消耗每个array1项,而对array2 When one request is done in one of these arrays, the next request starts if existent, else do nothing in array1 and when in array2 it just callbacks to the array1 to do the next array item request with the consume function. 当在这些数组之一中完成一个请求时,下一个请求(如果存在)开始,否则在array1不执行任何操作,在array2时,它仅回调到array1以使用消耗函数执行下一个数组项请求。

var len = array1.length; // Memorize array1 length

function consume(i) {
    var xhr = new XMLHttpRequest(); // Request
    xhr.open("GET",  "http://someurl1.ru/" + array1[i] + "/" , true);
    xhr.onreadystatechange = function() {
        if(this.readyState === 4) {
            // Status 200 is success
            if(this.status === 200) {
                // `this` invokes the `xhr` object
                // Your success block is here
                successCallback(i, this.responseText);
                // Consume next array1 items if length isn't ranged
            }else{
                // Error block is here; can be 403, 404, 500+, ... etc.
            }
        }
    }
    xhr.send()
}

consume(0);

function consume2(i, array2, array3, arrayLen, callback) {
    var xhr = new XMLHttpRequest(); // Request
    xhr.open("GET",  "http://someurl2.ru/" + array2[i] + "_" + array3[i] + "/", true);
    xhr.onreadystatechange = function() {
        if(this.readyState === 4) {
            // Status 200 is success
            if(this.status === 200) {
                // Your success block is here
                if(JSON.stringify(xhr.responseText).search(some_array[i]) !== -1){
                    var xhr2 = new XMLHttpRequest(); // Request
                    xhr2.open("GET", "http://someurl3.ru/" + array2[i] + "/" + array3[i] + "/" + some_another_array[i], true);
                    xhr2.onreadystatechange = function() {
                        if(this.readyState === 4) {
                            // Status 200 is success
                            if(this.status === 200) {
                                // Your success block is here
                                array4.push(xhr2.responseText);
                                // Consume next array2 items
                                if(i < len)
                                    consume2(++ i)
                                ;else
                                    callback()
                            }else{
                                // Error block is here; can be 403, 404, 500+, ... etc.
                            }
                        }
                    };
                    xhr2.send()
                }
            }else{
                // Error block is here; can be 403, 404, 500+, ... etc.
            }
        }
    }
    xhr.send()
}

function successCallback(f, data) {
    array2.length = 0;
    array3.length = 0;
    var br = someresult1.split('<br>');
    for (var b = 0; b < br.length; b++){
        var space = br[b].split(" ");
        array2.push(space[0]);
        array3.push(space[1]);
    }
    consume2(0, array2, array3, array2.length, function() {
        if(f < len)
            consume(++ f)
    })
}

I'll update this code yet, but I don't understand what you surely want to do with it. 我将更新此代码,但我不明白您确定要使用它做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM