Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How can I temporarily disable the onclick event listener after the event has been fired?

How can I temporarily disable the onclick event listener, (jQuery preferred), after the event has been fired?

Example:

After the user clicks on the button and fires this function below, I want to disabled the onclick listener, therefore not firing the same command to my django view.

$(".btnRemove").click(function(){    $(this).attr("src", "/url/to/ajax-loader.gif");    $.ajax({         type: "GET",         url: "/url/to/django/view/to/remove/item/" + this.id,         dataType: "json",         success: function(returned_data){             $.each(returned_data, function(i, item){               // do stuff                             });    } }); 

Thanks a lot,

Aldo

like image 474
aldux Avatar asked Dec 17 '09 13:12

aldux


People also ask

How do I turn off event listener?

The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

Why should you remove event listeners once they are no longer used?

Any registered event should be cleaned up when not needed anymore. There are several reasons for that. First, events can easily cause memory leaks, and second, it is also simple to create incorrect code with events.

Do event listeners get removed?

Event listeners can also be removed by passing an AbortSignal to an addEventListener() and then later calling abort() on the controller owning the signal.

How do I know if an event listener has been removed?

After your call to removeEventListener is made, go back and check your event listeners again. If it was successful, your event listener should no longer be set. Once you're done debugging, you can then resume code execution (F8).


2 Answers

There are a lot of ways to do it. For example:

$(".btnRemove").click(function() {     var $this = $(this);     if ($this.data("executing")) return;     $this         .data("executing", true)         .attr("src", "/url/to/ajax-loader.gif");     $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) {         // ... do your stuff ...         $this.removeData("executing");     }); }); 

or

$(".btnRemove").click(handler);  function handler() {     var $this = $(this)         .off("click", handler)         .attr("src", "/url/to/ajax-loader.gif");     $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) {         // ... do your stuff ...         $this.click(handler);     }); } 

We can also use event delegation for clearer code and better performance:

$(document).on("click", ".btnRemove:not(.unclickable)", function() {     var $this = $(this)         .addClass("unclickable")         .attr("src", "/url/to/ajax-loader.gif");     $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) {         // ... do your stuff ...         $this.removeClass("unclickable");     }); }); 

If we don't need to re-enable the handler after it has been executed, then we can use the .one() method. It binds handlers that are to be executed only once. See jQuery docs: http://api.jquery.com/one

like image 119
thorn0 Avatar answered Sep 28 '22 01:09

thorn0


For how long do you want to disable the click event listener? One way is to unbind the event listener using jQuery's unbind http://docs.jquery.com/Events/unbind.

But it's best-practice not to unbind an event only to rebind it later. Use a boolean instead.

var active = true; $(".btnRemove").click(function() {     if (!active) {         return;     }     active = false;     $(this).attr("src", "/url/to/ajax-loader.gif");     $.ajax({         type: "GET",         url: "/url/to/django/view/to/remove/item/" + this.id,         dataType: "json",         success: function(returned_data) {             active = true; // activate it again !             $.each(returned_data, function(i, item) {                 // do stuff                                    });         }     }); }); 

edit: to be safe you should also care about the other ajax completion routines (there are only three: success, error, complete see docs) or else active might stay false.

like image 33
RamboNo5 Avatar answered Sep 28 '22 00:09

RamboNo5