jQuery click events firing multiple times

后端 未结 25 2219
不知归路
不知归路 2020-11-22 11:01

I\'m attempting to write a video poker game in Javascript as a way of getting the basics of it down, and I\'ve run into a problem where the jQuery click event handlers are f

25条回答
  •  北海茫月
    2020-11-22 11:32

    https://jsfiddle.net/0vgchj9n/1/

    To make sure the event always only fires once, you can use Jquery .one() . JQuery one ensures that your event handler only called once. Additionally, you can subscribe your event handler with one to allow further clicks when you have finished the processing of the current click operation.

    var subscribeClickEvent = function() {$("#testDiv").one("click", ".testClass", clickHandler);};
    
    function clickHandler() {
      //... perform the tasks  
      alert("you clicked the button");
      //... subscribe the click handler again when the processing of current click operation is complete  
      subscribeClickEvent();
    }
    
    subscribeClickEvent();
    

提交回复
热议问题