ページの読み込み時に API を呼び出す必要があります。すべての AJAX 呼び出しが終了 (完了) したら、UI のロックを解除します。問題は、API を呼び出す回数がわからないことです。1回か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 を使用してそれを解決できますか?