使用Jquery解决所有承诺后执行代码

Execute code after all the promises have been resolved with Jquery

本文关键字:执行 代码 承诺 Jquery 解决 使用      更新时间:2023-09-26

我似乎很难在代码中找到如何使用when函数。我希望在完成所有ajax请求后实现window.location.reload

如果我把window.location.reload放在"each"循环之后,那么我所有的AJAX请求都会被中止。

$('.removeSelected').click(function() {        
    if (confirm('Are you sure you will continue?')) {
        $('.container-fluid :input:checked').each(function () {
            var recordId = $(this).data("id");
            // Jquery remove it         
            $.ajax({
                method: "GET",
                url: "/controllers/lines.asp",
                data: { recordId: recordId  }
            }).done(function() {
                console.log('done');
            });
        });
        // window location reload here
    }       
});

您的逻辑顺序似乎有缺陷。我认为你想做的是收集你所有的记录,然后发送一个请求。请求完成后,您将重新加载。

$('.removeSelected').click(function() {        
    if (confirm('Are you sure you will continue?')) {
        //prepare an array for records
        var records = [];
        $('.container-fluid input:checked').each(function () {
           //add the id
           records.push($(this).data("id"));
        });
        //make the request
        $.ajax({
            method: "GET",
            url: "/controllers/lines.asp",
            data: records
            }).done(function() {
               //when successful, reload the page
               window.location.reload();
            });
        });
    };
});

我不确定你是想在成功时重新加载还是在完成时重新加载(这也会在失败时重新加载)。

获取要使用删除的元素数量

$('.container-fluid :input:checked').size();

每次调用done时,分别递增一个总数。当总数与计数匹配时,重新加载页面。

我同意fauxserious的观点,即一次发送所有信息,而不是对每条记录发出请求,会更有效。

但是,如果出于任何原因您不想更改服务器端代码,您需要做的就是确定所有请求何时完成。你可以通过承诺来实现这一点。

如果您对此没有太多经验,下面的代码应该会让您了解如何在不使用promise的情况下实现预期行为:

$('.removeSelected').click(function() {        
    if (confirm('Are you sure you will continue?')) {
        var $containerFluidElements = $('.container-fluid :input:checked');
        var numberOfRequestsPending = $containerFluidElements.length;
        $containerFluidElements.each(function () {
            var recordId = $(this).data("id");
            $.ajax({
                method: "GET",
                url: "/controllers/lines.asp",
                data: { recordId: recordId  }
            }).done(function() {
                numberOfRequestsPending -= 1;
                if(numberOfRequestsPending == 0) 
                {
                    window.location.reload();
                }
            });
        });
    }       
});

您需要将所有承诺传递给"when",以便它可以等待所有承诺都得到解决。

为此,您可以将$.ajax返回的所有promise放在一个数组中,并将其传递给$.when:

 $('.removeSelected').click(function() {        
    if (confirm('Are you sure you will continue?')) {
    var promises = [];
    $('.container-fluid :input:checked').each(function () {
        var recordId = $(this).data("id");
        // Jquery remove it         
          promises.push($.ajax({
                method: "GET",
                url: "/controllers/lines.asp",
                data: { recordId: recordId  }
            })
                .done(function() {
                   console.log('done');
        }));
    });
    // use apply to pass an array instead list of parameters
    $.when.apply($, promises).then(function() {
        // all the promises have been resolved
        window.location.reload();
    }       
});