jQuery click events firing multiple times

后端 未结 25 2199
不知归路
不知归路 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:42

    The below code worked for me in my chat application to handle multiple mouse click triggering events more than once. if (!e.originalEvent.detail || e.originalEvent.detail == 1) { // Your code logic }

    0 讨论(0)
  • 2020-11-22 11:43

    We must to stopPropagation() In order to avoid Clicks triggers event too many times.

    $(this).find('#cameraImageView').on('click', function(evt) {
       evt.stopPropagation();
       console.log("Camera click event.");
    });
    

    It Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. This method does not accept any arguments.

    We can use event.isPropagationStopped() to determine if this method was ever called (on that event object).

    This method works for custom events triggered with trigger(), as well.Note that this will not prevent other handlers on the same element from running.

    0 讨论(0)
  • 2020-11-22 11:43
    $(element).click(function (e)
    {
      if(e.timeStamp !== 0) // This will prevent event triggering more then once
       {
          //do your stuff
       }
    }
    
    0 讨论(0)
  • 2020-11-22 11:46

    .one only fires once for the lifetime of the page

    So in case you want to do validation, this is not the right solution, because when you do not leave the page after validation, you never come back. Better to use

    $(".bet").on('click',function() 
    { //validation 
       if (validated) { 
          $(".bet").off('click'); //prevent to fire again when we are not yet off the page
          //go somewhere
        }
    });
    
    0 讨论(0)
  • 2020-11-22 11:47

    It happens due to the particular event is bound multiple times to the same element.

    The solution which worked for me is:

    Kill all the events attached using .die() method.

    And then attach your method listener.

    Thus,

    $('.arrow').click(function() {
    // FUNCTION BODY HERE
    }
    

    should be:

    $('.arrow').die("click")
    $('.arrow').click(function() {
    // FUNCTION BODY HERE
    }
    
    0 讨论(0)
  • 2020-11-22 11:47

    Try that way:

    <a href="javascript:void(0)" onclick="this.onclick = false; fireThisFunctionOnlyOnce()"> Fire function </a>
    
    0 讨论(0)
提交回复
热议问题