你可以做这样的事情:
$('#showResult').click(function () {
var _this = this;
if (this.inprogress) {
alert('please wait');
return;
}
this.inprogress = true;
$.getJSON (url, data, function updateForm() {
.... MY CODE ....
_this.inprogress= false;
});
});
但是通常我更喜欢显示进度微调器,并且当我进行长时间加载时整个窗口变灰,以便用户知道他必须等待:
loading = {
count: 0
};
loading.finish = function() {
this.count--;
if (this.count==0) this.$div.hide();
};
loading.start = function() {
this.count++;
if (!this.$div) {
var html = '<div style="position: fixed;z-index:100;left:0;top:0;right:0;bottom:0;background: black;opacity: 0.6;">'; // this class covers and greys the whole page
html += '<table width=100% height=100%>';
html += '<tr><td align=center valign=middle>';
html += '<img src=img/loading.gif>';
html += '</td></tr>';
html += '</table></div>';
this.$div=$(html);
this.$div.prependTo('body');
}
setTimeout(function(){
if (loading.count>0) loading.$div.show();
}, 500);
};
$('#showResult').click(function () {
loading.start();
$.getJSON (url, data, function updateForm() {
.... MY CODE ....
loading.finish();
});
});
(使用此代码,仅当 ajax 调用时间超过 500 毫秒时才会显示微调器)。