The main problem here is that the this
inside the ajax function is not the same as the this
out of the function. The scope is different... This has easy solution, which is to create an intermediate variable that points to the same object:
$("#meuBotao").click(function (e) {
$(this).off("click");
var self = this; // assim guardamos o apontador no "self"
$.ajax({
url: "/page",
type: "POST",
complete: function() {
$(self).on("click"); // aqui usa o "self". Já agora, aqui falta a função a correr quando o click fôr gerado
},
...
Apart from this detail there are other more commonly used ways to prevent clicks, rather than removing and adding events. One of them is to create a flag/flag as property in the element or a variable that stores the information of whether the element can be clicked or not.
An example of this would be:
$("#meuBotao").click(function (e) {
if (!$(this).data('clicavel')) return false;
$(this).data('clicavel', false);
var self = this; // assim guardamos o apontador no "self"
$.ajax({
url: "/page",
type: "POST",
complete: function() {
$(self).data('clicavel', true);
},
...
You can even do the same by adding CSS pointer-events: none;
in a class, or directly in the element. You can see more ideas in this answer: /a/2353/129
Note: in your code $(this).on("click");
the second parameter of the function indicating which code to run when the click is received is missing...
In this case it makes no difference to use
click
oron
.– bfavaretto
@bfavaretto In fact, giving a boring, has yes, it is proven that and method
.on()
is plus performatic, therefore, even without taking full advantage of the functionalities of this method, it is still advantageous to use it.– Kazzkiq
The very test you linked shows that this is not true. There is only a difference in performance between using delegation or not.
– bfavaretto
I think use
.on
or.click
in this case is irrelevant and I do not believe I am more recommended here. The.on
how linkou is used to delegate events, which does not apply here.– Sergio
@Sergio o
.on
is not limited to the use with delegation of events, in fact the use of it in place of ancient delegates is even encouraged in the blog itself jQuery.– Kazzkiq