DEVOPS
DEVOPS

Reputation: 18790

How to disable a click event for some time using jquery?

I have a left tree. Its structure is like this


    MainCategory
      Category1
        Subcategory1
        Subcategory2
      Category2
        Subcategory3
        Subcategory4
        Subcategory5
      Category3
      .
      .. etc like that      

If user click any of the MainCategory/Category/Subcategory I need to disable/prevent repeated/multiple click on same link until the result come. How can I do that using jquery?

Upvotes: 3

Views: 2736

Answers (2)

Jayendra
Jayendra

Reputation: 52809

May be just use a class to check if its already clicked.
If clicked, do not execute, and remove the class on success and failure.

Prototype -

$('.selector').click(function(){
    if(!$(this).hasClass('someclass')){
        $(this).addClass('someclass');
        $.ajax({
          url: "test.html",
          success: function(){
            $(this).removeClass("someclass");
          },
          error: function(){
            $(this).removeClass("someclass");
          }
        });
    }
})

Upvotes: 1

Manuel van Rijn
Manuel van Rijn

Reputation: 10315

if you use jQuery 1.7 you can use the off() and on() functionality

example:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body").on("click", "p", foo);


// ... foo will no longer be called.
$("body").off("click", "p", foo); 

if you use an older version you could unbind() and bind() the event

example:

var foo = function () {
    // code to handle some kind of event
};

// ... now foo will be called when paragraphs are clicked ...
$("body p").bind("click", foo);

// ... foo will no longer be called.
$("body p").unbind("click");

Upvotes: 2

Related Questions