当我的 ajax 调用检索数据时,我收到此加载消息。但我得到了奇怪的结果。消息要么出现并渲染一半,直到 ajax 完成,要么根本不出现,让用户想知道出了什么问题。我需要加载消息的原因是,在检索数据、打开对话框、绘制 map 、然后使用标签重新绘制 map 的要素图层时,会有大约 5-10 秒的延迟。
这是我的代码:
function loadData(v)
{
var reg = 1;
var vId = v;
var d =
{
regionType: reg,
varId: vId
};
//$("#loading").ajaxStart(function () {
// $(this).show();
//}).ajaxStop(function () {
// $(this).hide();
//});
$("#loading").ajaxStart(function () {
$(this).show();
});
$.ajax({
type: "GET",
url: WebRoot + "ws/bis.asmx/Data",
data: d,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
fipsData = data.d;
openBox(d);
init(regType, varId);
$("#loading").ajaxStop(function () {
$(this).hide();
});
} //ends success function
}); //ends ajax call
}; //ends message
最佳答案
不需要ajaxStart
或ajaxStop
function loadData(v)
{
var reg = 1;
var vId = v;
var $loading = $("#loading");
var d =
{
regionType: reg,
varId: vId
};
// Starts immediately after this line so no need to use ajaxStart
$loading.show();
$.ajax({
type: "GET",
url: WebRoot + "ws/bis.asmx/Data",
data: d,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
fipsData = data.d;
openBox(d);
init(regType, varId);
}, //ends success function
// Fires even if a failure, so loading spinner won't hang around for no reason
done: function() {
$loading.hide();
}
}); //ends ajax call
}; //ends message
关于ajax - 使用 jquery 加载消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18540952/