次のようなことができます:
$('#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 ミリ秒以上かかる場合にのみスピナーが表示されます)。