你可以做這樣的事情:
$('#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毫秒時才顯示微調器)。
可以禁用按鈕,當成功獲取數據,您啓用 – Ricky 2012-07-16 11:57:22