当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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();
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: