[英]Loading animation disappear before second ajax call
I am showing loading animation whenever ajax call gets started and stops once the ajax call is completed. 每当ajax调用开始时,我正在显示加载动画;一旦ajax调用完成,我将停止动画。 It works fine when i use only one ajax call in function.
当我在函数中仅使用一个ajax调用时,它可以正常工作。 However if i have two ajax calls within same function, it seems like the loading animation disappears after first ajax call is complete.
但是,如果我在同一函数中有两个Ajax调用,则在第一个Ajax调用完成后,加载动画似乎消失了。 How can i make it stay until all ajax call is complete?
我如何让它保持到所有ajax调用完成?
Thanks. 谢谢。
Javascript in main.js file: main.js文件中的Javascript:
//Show loading image on Ajax Start
$(document).ajaxStart(function() {
showPleaseWait();
});
//Hide loading image on Ajax complete
$(document).ajaxComplete(function() {
hidePleaseWait();
});
Javascript in Employee page: 员工页面中的Javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#ddlEmployeeNumber").change(function () {
$("#log").ajaxError(function (event, jqxhr, settings, exception) {
alert(exception);
});
var employeeNumberSelected = $("select option:selected").first().text();
$.get('@Url.Action("EmployeeSelfServiceInfo")',
{ chosenEmployeeNumber: employeeNumberSelected }, function (data) {
$("#EmployeeSelfServiceInfo").html(data);
});
$.get('@Url.Action("GetEmployeeName")',
{ chosenEmployeeNumber: employeeNumberSelected }, function (data) {
$("#employeeName").text(data);
});
});
});
</script>
function showPleaseWait() {
var modalLoading = '<div class="modal" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false role="dialog">\
<div class="modal-dialog">\
<div class="modal-content">\
<div class="modal-header">\
<b>Processing...</b>\
</div>\
<div class="modal-body center-block">\
<div class="progress">\
<div class="progress-bar progress-bar-striped active" role="progressbar"\
aria-valuenow="100" aria-valuemin="100" aria-valuemax="100" style="width:100%;">\
Please Wait...\
</div>\
</div>\
</div>\
</div>\
</div>\
</div>';
$(document.body).append(modalLoading);
$("#pleaseWaitDialog").modal("show");
}
function hidePleaseWait() {
$("#pleaseWaitDialog").modal("hide");
}
UPDATE: So i got it to fixed by replacing existing .ajaxComplete call with .ajaxStop call. 更新: 所以我通过将现有的.ajaxComplete调用替换为.ajaxStop调用来解决此问题。
You may keep a variable to track mutiple ajax calls. 您可以保留一个变量来跟踪多个ajax调用。
var ajaxCallCounter=0;
//Show loading image on Ajax Start
$(document).ajaxStart(function() {
showPleaseWait();
});
//Hide loading image on Ajax complete
$(document).ajaxComplete(function() {
if(ajaxCallCounter===0)
{
hidePleaseWait();
}
});
Every time you make an ajax call, increase this variable. 每次进行ajax调用时,请增加此变量。 When your ajax call gets a response from server, decrease it.
当您的ajax调用从服务器获得响应时,请减少响应。
ajaxCallCounter++;
$.get('@Url.Action("EmployeeSelfServiceInfo")',function(res){
ajaxCallCounter--;
//do something with the res
});
In the above example, we used a simple global variable ( Which is not so good ) !.You may use javascript namespacing for declaring the variable to prevent possible overwriting of the value of this variable or wrap your code inside an IIFE . 在上面的示例中,我们使用了一个简单的全局变量(效果不太好 !)。您可以使用javascript名称空间声明该变量,以防止该变量的值可能被覆盖或将代码包装在IIFE中 。
You can use a counter. 您可以使用计数器。
Basic idea: 基本思路:
(function(){
var openCount = 0;
$(document).ajaxStart(function() {
openCount++;
showPleaseWait();
});
$(document).ajaxComplete(function() {
openCount--;
if(!openCount) hidePleaseWait();
});
}());
don't use ajaxStart and ajaxComplete, you can bind those calls to each $.get
instead: 不要使用ajaxStart和ajaxComplete,可以将这些调用绑定到每个
$.get
:
$.get('@Url.Action("EmployeeSelfServiceInfo")',
{ chosenEmployeeNumber: employeeNumberSelected },
beforeSend: showPleaseWait,
complete: hidePleaseWait,
function (data) {
$("#EmployeeSelfServiceInfo").html(data);
});
$.get('@Url.Action("GetEmployeeName")',
{ chosenEmployeeNumber: employeeNumberSelected },
beforeSend: showPleaseWait,
complete: hidePleaseWait,
function (data) {
$("#employeeName").text(data);
});
The problem is that since those are async calls, the hide/show on those elements will look weird, probably. 问题在于,由于这些调用是异步调用,因此这些元素上的隐藏/显示可能看起来很奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.