jlocker
jlocker

Reputation: 1488

How to stop .click function being fired many times if user clicks many times

How to stop .click() function being fired many times if user clicks many times.

Here is my code

$("#loadme").unbind('click').click(function() {
  hello();
});

If the user clicks on #loadme many times the function will repeat again and again. I want it to stop firing it many times.

Upvotes: 0

Views: 92

Answers (3)

Thom-x
Thom-x

Reputation: 866

You can do :

$("#loadme").on("click",function(){
    $("#loadme").off("click");
    hello();
});

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382150

You seem to want one :

$("#loadme").one('click', function(){
   hello();
});

Upvotes: 4

Ohgodwhy
Ohgodwhy

Reputation: 50787

var clicked = false;

$("#loadme").unbind('click').click(function(){
    hello();
    clicked = true;
});

function hello(){
    if(clicked){
        //do nothing because it was clicked more than once.
    }else{
        //your code
        clicked = false; //reset
    }
}

Upvotes: 0

Related Questions