多个Ajax在.each循环中运行syncronus

时间:2015-05-20 10:04:16

标签: javascript php jquery ajax

我希望多次运行AJAX请求以从不同的文件夹中获取数据,最后一个AJAX将触发以生成数据的压缩文件。 一切都已完成,但最大的问题是文件文件运行后会运行一些文件,因此它们不能包含在zip文件中。 请建议做什么?

$('#View_Rep').click(function() {
    $(this).hide();
    $('#restable').hide();
    document.adminweeklycorrection.action = '';
    $('#centerSec').css('display', 'none');
    $('#processingDiv').css('display', 'block');
    $('#processingDiv').html("<img height='150' src='img/loading-circle.gif'><h4>Processing data</h4>");
    var carr = $('#wkarr').val();
    carr = carr.split(",");
    $.each(carr, function(n) {
        if ($('#weekarray').val()) {
            $.ajax({
                url: "rxml.php",
                method: "POST",
                async: "false",
                data: $("#adminweeklycorrection").serialize()
            }).done(function(msg) {
                if (n == parseFloat(carr.length) - parseFloat(1)) {
                    $('#processingDiv').html("<img height='150' src='img/loading-circle.gif'><h4>Preparing reports</h4>");
                    $.ajax({
                        url: "cmbrxml.php",
                        method: "POST",
                        async: "false",
                        data: $("#adminweeklycorrection").serialize()
                    }).done(function(msg) {
                        $('#processingDiv').html("<h3>Report generated successfully</h3><h4><a href='" + msg + "'>Download Report</a></h4><h5><a href='adminchartcorrection.php'>Back</a></h5>");
                        if ($('#logtype').val() == 'Ship') {
                            $('#processingDiv').append("<form name='send_mail' id='send_mail' method='post' action='adminchartcorrection.php'><input type='hidden' name='chartnumber' id='chartnumber' value='" + $('#chartnumber').val() + "'><input type='submit' name='send_mail' value='Send Mail' id='send_mail'></form>");
                        }
                        //redir(msg);
                    });
                }
            });
            var curr = $('#weekarray').val();
            curr = curr.split(",");
            var sftd = curr.shift();
            //$('#weekarray').val('');
            $('#weekarray').val(curr);
        }
    });
});

2 个答案:

答案 0 :(得分:1)

检查循环索引是否是最后一个并不意味着相关的ajax请求是最后一个执行的。而不是那样,使用请求计数变量,并在每个请求的完成方法上减少它。

试试这个:

var ajaxRequestsLeft = carr.length;        //added
$.each(carr, function(n) {
    if ($('#weekarray').val()) {
        $.ajax({
            url: "rxml.php",
            method: "POST",
            async: "false",
            data: $("#adminweeklycorrection").serialize()
        }).done(function(msg) {
            ajaxRequestsLeft--;            //added
            if(ajaxRequestsLeft == 0) {    //modified
                $('#processingDiv').html("<img height='150' src='img/loading-circle.gif'><h4>Preparing reports</h4>");
                $.ajax({
                    url: "cmbrxml.php",
                    method: "POST",
                    async: "false",
                    data: $("#adminweeklycorrection").serialize()
                }).done(function(msg) {
                    $('#processingDiv').html("<h3>Report generated successfully</h3><h4><a href='" + msg + "'>Download Report</a></h4><h5><a href='adminchartcorrection.php'>Back</a></h5>");
                    if ($('#logtype').val() == 'Ship') {
                        $('#processingDiv').append("<form name='send_mail' id='send_mail' method='post' action='adminchartcorrection.php'><input type='hidden' name='chartnumber' id='chartnumber' value='" + $('#chartnumber').val() + "'><input type='submit' name='send_mail' value='Send Mail' id='send_mail'></form>");
                    }
                    //redir(msg);
                });
            }
        });
var curr = $('#weekarray').val();
curr = curr.split(",");
var sftd = curr.shift();
        //$('#weekarray').val('');
        $('#weekarray').val(curr);
    }
});

答案 1 :(得分:0)

我的建议是发出异步请求(它们会同时触发,而不必等到上一次完成)。

并使用承诺。 (我不熟悉jquery promises所以会给出抽象的例子):

var promises = [];
$.each(arr....
    // as I said I'm not sure if $.ajax returns promise in jquery (I use angular). This is just abstract example
    var promise = $.ajax(.... async: TRUE ....)
    promises.push(promise);
});

// again, this is just abstract example, but promises library that you'll use will have a way to register a callback when array of promises are finished
$q.all(promises).then(function(responses) {
    // now ALL requests are finished and you have ALL responses in "responses array
});