ajax运行时显示进度条或旋转器
Show Progress bar or spinner when ajax is running
我试图在ajax运行时添加进度条或旋转器,但进度条和旋转器只会与ajax的成功警报同时出现。
注意:我使用JSP, ajax用于通过servlet更新数据库数据。进度条正在使用bootstrap。
$('#sync').click(function () {
var start = $('#startdate').val();
var end = $('#enddate').val();
if ((start !== "") && (end !== "")) {
var startDate = new Date(start);
var endDate = new Date(end);
$('#cover').show();
$.ajax({
type: "post",
url: "PRSync",
data: "start=" + startDate.yyyymmdd() +
"&end=" + endDate.yyyymmdd(),
async: false,
beforeSend: function () {
},
success: function (data) {
var arr = data.split('|');
if (arr[0] === "0") {
alert("Sync fail");
} else {
alert("Sync Succesfull. Total rows of " + arr[1] + " affected.");
}
},
error: function (jqXHR, textStatus, errorThrown, err4) { // if error occured
alert(jqXHR + " _ " + textStatus + " _ " + errorThrown + " _ " + err4);
},
complete: function () {
$('#cover').fadeOut(1000);
}
});
}
});
$(document).ajaxStart(function () {
// show loader on start
showbusy();
}).ajaxSuccess(function () {
// hide loader on success
hidebusy();
});
Date.prototype.yyyymmdd = function () {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding
};
var myApp;
myApp = myApp || (function () {
var pleaseWaitDiv = $('<div class="modal fade" style=top:40% id="pleaseWaitDialog" data-backdrop="false" data-keyboard="false"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title">Synchronizing is processing</h4></div><div class="modal-body"><div class="progress"><div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div></div></div></div></div></div>');
return {
showPleaseWait: function () {
pleaseWaitDiv.modal();
},
hidePleaseWait: function () {
pleaseWaitDiv.modal('hide');
},
};
})();
function showbusy() {
myApp.showPleaseWait();
}
function hidebusy() {
myApp.hidePleaseWait();
}
你试过了吗:
$(document).bind("ajaxStart", function(){
showbusy(); })
.bind("ajaxStop", function(){
hidebusy();
});
显然,如果你的ajax调用处理得非常快,可能看起来你的函数被"延迟"触发,但是通过将事件绑定到$(document)
,你可以确保它们不会只触发一次。
如果仍然不能满足您的需要,我将把这些函数放在ajax调用中:
$.ajax({
beforeSend: showbusy(),
complete: hidebusy()
});
你可以使用jquery. blocku .js.
你可以在你的代码中这样初始化它:
$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- 如何在运行时在angular 2中加载外部js脚本
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- http.listen()在运行时接受终端命令
- 自定义运行时Can'在谷歌应用引擎中看不到我的自定义日志
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- JavaScript运行时是如何工作的
- 在运行时创建元素时移到一边时出错
- 如何在运行时在HTML5画布中绘制正方形
- 如何在运行时使用javascript隐藏图像
- 在运行时使用jquery准备表体会导致设计问题
- 使用主题运行时portlet liferay时出现Javascript错误
- JavaScript运行时事件循环现有技术
- Appcelerator Titanium:在运行时下载并解释JavaScript代码
- angularjs ng点击运行时标记不起作用
- 在函数运行时显示对话
- 如何在javascript中在运行时获取对象/数组
- 变量在运行时未定义,但在使用调试器时定义
- ajax运行时显示进度条或旋转器