jquery - 如何知道所有 AJAX 调用何时完成?

标签 jquery

在页面加载时我需要调用 API。当所有 AJAX 调用完成(完成)时,我想要解锁 UI。问题是我不知道我会调用API多少次。可能是一次或 10 次。

我的代码:

$(document).ready(function () {

           callAPI(yourApiKey);
});

及功能:

function getRates(yourAPIKey, contractCode, startDate, endDate) {
// I did cut some code here
 $.ajax({   
     url: ratesEnquiry,
     type: 'GET',
     dataType: "jsonp",
     jsonp: "callback",
     complete: function (response, responseCode) {       
     },
     success: function (json) {
     $.each(json, function (index, value) {
             populateValues("rate", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.RoomPrice);
             populateValues("hrate", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.RoomPrice);

             });
         }
     });
 }



function getAvailability(yourAPIKey, contractCode, startDate, endDate) {
    // I've cut off some code here as well
    $.ajax({
    url: availabilityEnquiry,
    type: 'GET',
    dataType: "jsonp",
    jsonp: "callback",
    //jsonpCallback: "jsonpCallback",
    complete: function (json, responseCode) {
        //console.log(response); console.log(responseCode);
        //alert("complete");           
    },
    success: function (json) {

        $.each(json, function (index, value) {
             populateValues("avail", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.Quantity);
             populateValues("havail", this.ContractCode, this.RoomTypeCode, this.Date.substr(0, 10), this.Quantity);                      
            // alert(this.RoomPrice);
            });

        }
    });
}


function callAPI(yourAPIKey) {

     // I've cut off some code here

     $.blockUI({ message: '<span class="loader green" original-title="1Loading, please wait…"></span><h3> Please wait...</h3>' });
     $.ajax({
         url: enquiry,
         type: 'GET',
         dataType: "jsonp",
         jsonp: "callback",
         complete: function (response, responseCode) {
         },
         success: function (json) {
             $.each(json.Contracts, function (index, contract) {
                ContractsArray[Count] = contract.ContractCode;
                Count++;

             });
             for(var i = 0; i < Count; i++){

                 getAvailability(yourAPIKey, ContractsArray[i], startDate, endDate);
                 getRates(yourAPIKey, ContractsArray[i], startDate, endDate);

             }
         }
     });
    }

我可以使用 ajaxStop 或 ajaxComplete 来解决这个问题吗?

最佳答案

使用计数器并检查是否每次调用都会向该计数器加 1,每次完成时该计数器是否会减 1,如果计数器为 0,则您知道所有 api 调用都已完成 简单的例子:

var calls = 0;
function makeCall(url) {
   calls++;
   $.ajax({
      complete: function(resp) {
          calls--;
      }
   });
}

关于jquery - 如何知道所有 AJAX 调用何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11972489/

相关文章:

jquery - 如何使用 jquery 操作 HTML 元素?

javascript - jQuery Cleditor 所见即所得文本编辑器 : keyup() works in webkit browsers but not Firefox or IE

android - 在单个 html 文件中使用 jQuery 更新一页

javascript - 在最后一个 child 之前插入 jQuery

jquery - CSS 过渡显然不适用

java - JQPlot Ajax图形渲染问题

php - jquery .eq 不适用于某些导航项目

javascript - 如何比较两个 JSON 具有相同的属性而无需排序?

javascript - 动态添加 jQuery 的 slider

javascript - setTimeout() ajax 请求无法成功运行