5
A
回答
5
3nigma是在正确的轨道上,但得到了一个细节错误,至少在一般情况下。
使用ajaxSetup只提供默认值,如果稍后你做一些ajax调用,指定他们自己的beforeSend回调(即你需要设置一些特定的头文件)或完成(你想同时处理成功和错误),他们会覆盖ajaxSetup中的那些,并且您的加载指示符将会中断。
相反,使用Global Ajax Events(more about ajax events)
$(document).ajaxSend(function(e, jqXHR){
//show the loading div here
});
$(document).ajaxComplete(function(e, jqXHR){
//remove the div here
});
这是即使其他代码也想这样设置一些全局或局部beforeSend /完整的处理程序或调用ajaxSetup,不会打破一个更通用的解决方案。
6
$("#loading").show(); //before send
$.get('/Stuff.php', function (data) {
$('#own').html(data);
$("#loading").hide(); //when sucess
});
+1
对此的一个调整将使用.delay(500).show()和.stop(true ).hide(),以便它只显示'加载',如果它需要超过500毫秒(或其他)返回。 – izb 2012-02-06 09:36:43
8
$.ajaxSetup({
beforeSend:function(xmlHttpRequest){
//show the loading div here
},
complete:function(){
//remove the div here
}
});
现在做Ajax调用
$.get('/Stuff.php', function (data) {
$('#own').html(data);
});
4
您的jQuery的使用beforeSend
和complete
功能。在beforeSend
中显示您的控件,并在complete
上隐藏它。
0
像刚刚完成简单:
<style type="text/css">
#loadingbar {
position: fixed;
z-index: 2147483647;
top: 0;
left: -6px;
width: 1%;
height: 2px;
background: #b91f1f;
-moz-border-radius: 1px;
-webkit-border-radius: 1px;
border-radius: 1px;
-moz-transition: all 500ms ease-in-out;
-ms-transition: all 500ms ease-in-out;
-o-transition: all 500ms ease-in-out;
-webkit-transition: all 500ms ease-in-out;
transition: all 500ms ease-in-out;
}
#loadingbar.waiting dd, #loadingbar.waiting dt {
-moz-animation: pulse 2s ease-out 0s infinite;
-ms-animation: pulse 2s ease-out 0s infinite;
-o-animation: pulse 2s ease-out 0s infinite;
-webkit-animation: pulse 2s ease-out 0s infinite;
animation: pulse 2s ease-out 0s infinite;
}
#loadingbar dt {
opacity: .6;
width: 180px;
right: -80px;
clip: rect(-6px,90px,14px,-6px);
}
#loadingbar dd {
opacity: .6;
width: 20px;
right: 0;
clip: rect(-6px,22px,14px,10px);
}
#loadingbar dd, #loadingbar dt {
position: absolute;
top: 0;
height: 2px;
-moz-box-shadow: #b91f1f 1px 0 6px 1px;
-ms-box-shadow: #b91f1f 1px 0 6px 1px;
-webkit-box-shadow: #B91F1F 1px 0 6px 1px;
box-shadow: #B91F1F 1px 0 6px 1px;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
border-radius: 100%;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({
beforeSend:function(xmlHttpRequest){
if ($("#loadingbar").length === 0) {
$("body").append("<div id='loadingbar'></div>")
$("#loadingbar").addClass("waiting").append($("<dt/><dd/>"));
$("#loadingbar").width((50 + Math.random() * 30) + "%");
}
//show the loading div here
},
complete:function(){
$("#loadingbar").width("101%").delay(200).fadeOut(400, function() {
$(this).remove();
});
//remove the div here
}
});
});
</script>
把它们到你的页面,当你永远使AJAX调用加载会显示.. 演示我的网站上进行了测试:http://www.xaluan.com
0
Bootstrap模型。
var loadingPannel;
loadingPannel = loadingPannel || (function() {
var lpDialog = $("" +
"<div class='modal' id='lpDialog' data-backdrop='static' data-keyboard='false'>" +
"<div class='modal-dialog' >" +
"<div class='modal-content'>" +
"<div class='modal-header'><b>Processing...</b></div>" +
"<div class='modal-body'>" +
"<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>");
return {
show: function() {
lpDialog.modal('show');
},
hide: function() {
lpDialog.modal('hide');
},
};
})();
Ajax调用
$.ajax({
url: "/",
type: "POST",
data: responseDetails,
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
beforeSend: function() {
loadingPannel.show();
},
complete: function() {
loadingPannel.hide();
},
data: responseDetails
})
.done(function (data) {
if (data.status == "Success") {
//Success code goes here
}
相关问题
- 1. 使用jQuery AJAX使得两个请求
- 2. 如何使用ajax请求显示表
- 3. 使用jQuery的AJAX请求
- 4. jquery,如何使用多个Ajax请求?
- 5. 使用jQuery显示AJAX请求的结果
- 6. Ajax多个请求在同一时间
- 7. Jquery ajax请求显示旧的响应
- 8. 多个AJAX请求使用时
- 9. 用jquery超时Ajax请求?
- 10. 使用单个AJAX请求同时保存多个记录?
- 11. 使用同一个会话创建多个AJAX请求
- 12. jquery ajax同步http请求
- 13. jQuery如何在async为true时使ajax请求“同步”
- 14. jquery ajax($ .post)在请求正在处理时显示一个图标
- 15. jQuery的Ajax请求只能使用一个参数
- 16. 使用jquery元素属性创建一个ajax请求
- 17. 使用jQuery的AJAX POST请求
- 18. 在Rails中使用jQuery的Ajax请求
- 19. 使用Codeginiter的jQuery Mobile的Ajax请求
- 20. 可重复使用的jquery ajax请求
- 21. 使用jsonp错误的jQuery ajax请求
- 22. jQuery使用Javascript提醒的Ajax请求()
- 23. 使用JQuery和Ajax获取HTTP请求
- 24. 使用jQuery从AJAX请求中断HTML
- 25. Json使用Ajax劫持Jquery post请求
- 26. 使用jsonp制作ajax请求(无jquery)
- 27. 跨域Ajax请求使用jQuery/PHP
- 28. ajax显示2个数据请求onclick
- 29. 在Ajax请求中使用同步
- 30. 使用jQuery使用不同输入变量的多个Ajax HTTP GET请求
你是什么意思 “没有阻止它” 是什么意思? – kapa 2012-02-06 09:17:11
可能的重复:http://stackoverflow.com/questions/1430438/how-to-call-jquery-ajaxstart-ajaxcomplete – 2012-02-06 09:18:44