只要所有函数都加载,我就尝试使用ajaxStart
和ajaxStop
来显示加载程序。
我查阅所有网址并向每个网址发送请求,所以直到他们不加载所有不要隐藏加载程序。
这是我的代码:
$.ajax({
url: getBaseUri() + 'dashboard/index',
method: 'GET',
success:function(data){
var datas = data['return'];
var urls = [];
var idDash = [];
var types = [];
/*
* I go through the data to send the data of each dashboard
*/
for (var i in datas) {
//Urls, dashboard ids and descriptions are saved in an array
urls.push({
url: datas[i].route,
id: datas[i].id,
title: datas[i].privilege,
div: datas[i].div
});
idDash.push(datas[i].id);
types.push(datas[i].type);
/*
* I send the parameters to the receiveData ()
*/
receiveData(datas[i].route, datas[i].sign, datas[i].class, datas[i].div, datas[i].privilege, datas[i].type, types, idDash, datas[i].id, urls, datas[i].label, datas[i].xaxis, datas[i].yaxis, datas[i].background);
}
},
error: function(error){
console.log(error);
}
});
function receiveData(url, sign, iconClass, div, title, type, types, idDash, id, urls, label, xaxis, yaxis, background) {
$.ajax({
url: url,
method: 'GET',
success: function(data){
var datas = data['return'];
if (type === "Bar") {
barChart(datas, title, div, type, types, idDash, id, urls);
}
if (type === "Indicator") {
indicatorsChart({
data: datas,
div: div,
title: title,
icon: sign,
class: iconClass,
idDash: id
});
}
if (type === "Sowing") {
sowingIndicator({
data: datas,
div: div,
title: title,
idDash: id
});
}
if (type === "BarChart") {
barCharts({
data: datas,
div: div,
title: title,
url: url,
label: label,
xaxis: xaxis,
yaxis: yaxis,
type: sign,
background: background
});
}
},
error: function(error){
console.log(error);
}
});
}
我已经使用了它,但它不起作用:
$(".loader").bind("ajaxStart", function () {
$(".loader").show();
}).bind("ajaxComplete", function () {
$(".loader").hide();
});
我不知道自己做错了什么,我会感激任何帮助。
谢谢!
答案 0 :(得分:0)
您没有绑定到正确的元素 - 您通常应该将ajaxStart
绑定到文档本身:
$(document).bind("ajaxStart", function () {
$(".loader").show();
}).bind("ajaxComplete", function () {
$(".loader").hide();
});
一个JSFiddle证明了这一点: http://jsfiddle.net/x196bb4L/1/
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以在触发ajax调用时显示加载程序,并将其隐藏在ajax回调上,如下所示:
var loader = $('.loader');
loader.show();
$.ajax({
...
success: function(data) {
loader.hide();
},
error: function() {
loader.hide();
}
});