只要所有功能都已加载,我就尝试使用ajaxStart
并ajaxStop
显示一个加载器。
我查询所有URL并向每个URL发送一个请求,因此,在不加载所有URL之前,请不要隐藏加载器。
这是我的代码:
$.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();
});
我不知道自己做错了什么,我将不胜感激。
谢谢!
您没有绑定到正确的元素-通常应该绑定ajaxStart
到文档本身:
$(document).bind("ajaxStart", function () {
$(".loader").show();
}).bind("ajaxComplete", function () {
$(".loader").hide();
});
还有一个JSFiddle演示了这项工作:http : //jsfiddle.net/x196bb4L/1/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句