简体   繁体   中英

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.

You seem to want one :

$("#loadme").one('click', function(){
   hello();
});
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
    }
}

You can do :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM