a/22078073/1636522
HTML
<button id="on">.on('click', click) (0)</button><br />
<button id="one">.one('click', click) (0)</button><br />
<button id="fakeAjax">.one('click', fakeAjax) (0)</button>
CSS
body {
font: normal 16px Arial;
}
button {
font: normal 16px Consolas,Courier;
line-height: 200%;
margin: 10px;
}
JavaScript
$('#on').on('click', click);
$('#one').one('click', click);
$('#fakeAjax').one('click', fakeAjax);
function click() {
$(this).text(function (i, text) {
return text.replace(/\((\d+)\)/, function ($0, $1) {
return '(' + (++$1) + ')';
});
});
}
function fakeAjax() {
var el = $(this);
click.call(this); // +1
el.next('span').remove(); // removes "done"
el.after('<span style="color:red">wait</span>'); // displays "wait"
setTimeout(function () { // waits 1,5 sec (fake Ajax)
el.next('span').remove(); // removes "wait"
el.after('<span>done</span>'); // displays "done"
el.one('click', fakeAjax); // re-binds a single click
}, 1500);
}