Prevent click event from firing when dblclick event fires

前端 未结 12 1160
名媛妹妹
名媛妹妹 2020-12-02 20:17

I\'m handling both the click and dblclick event on a DOM element. Each one carries out a different command, but I find that when double clicking on the element, in addition

相关标签:
12条回答
  • 2020-12-02 20:38

    In case anyone else stumbles on this (as I did) looking for an answer, the absolute best solution that I could come up with is the following:

        $node.on('click',function(e){
          if(e.originalEvent.detail > 1){
             return;
            /* if you are returning a value from this
             function then return false or cancel 
             the event some other way */
          }
        });
    

    Done. If there is more than one click back to back, the second, third,etc. will not fire. I definitely prefer this to using any sort of timers.

    I got myself pointed in this direction by reading this.


    Incidentally: I was first researching this problem because I accidentally double clicked a paginated link, and the event fired and finished twice before the callback could happen.

    Before coming up with the code above, I had

     if e.originalEvent.detail === 2 //return
    

    however, I was able to click on the link 3 times (a triple click), and though the second click didn't fire, the third did

    0 讨论(0)
  • 2020-12-02 20:39

    In this case, it is best to delay the execution of the single click event slightly. Have your double click handler set a variable that the single click event will check. If that variable has a particular value, could be boolDoubleClick == true, then don't fire/handle the single click.

    0 讨论(0)
  • 2020-12-02 20:43

    I use this solution for my project to prevent click event action, if I had dblclick event that should do different thing.

    Note: this solution is just for click and dblclick and not any other thing like tripleclick or etc.

    To see proper time between click and double click see this

    sorry for my bad English. I hope it helps :)

    var button, isDblclick, timeoutTiming;
    var clickTimeout, dblclickTimeout;
    //-----
    button = $('#button');
    isDblclick = false;
    /*
    the proper time between click and dblclick is not standardized,
    and is cutsomizable by user apparently (but this is windows standard I guess!)
    */
    timeoutTiming = 500;
    //-----
    button.on('dblclick', function () {
    
      isDblclick = true;
      clearTimeout(dblclickTimeout);
      dblclickTimeout = setTimeout(function () {
        isDblclick = false;
      }, timeoutTiming);
      //-----
      // here goes your dblclick codes
      console.log('double clicked! not click.');
      
    }).on('click', function () {
    
      clearTimeout(clickTimeout);
      clickTimeout = setTimeout(function () {
        if(!isDblclick) {
          // here goes your click codes
          console.log('a simple click.');
        }
      }, timeoutTiming);
      
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button type="button" id="button">
    click/dblclick on this to see the result
    </button>

    0 讨论(0)
  • 2020-12-02 20:45

    Here is my simple solution to prevent the second click. Of course, I could restart the timeout when a double click detected, but in reality I never need it.

    clickTimeoutId = null;
    
    onClick(e) {
        if (clickTimeoutId !== null) {
            // Double click, do nothing
            return;
        }
    
        // Single click
        // TODO smth
    
        clickTimeoutId = setTimeout(() => {
            clearTimeout(clickTimeoutId);
            clickTimeoutId = null;
        }, 300);
    }
    
    0 讨论(0)
  • 2020-12-02 20:47

    Here is what I did to distinguish within a module

           node.on('click', function(e) {
    
                //Prepare for double click, continue to clickHandler doesn't come soon enough
                console.log("cleared timeout in click",_this.clickTimeout);
                clearTimeout(_this.clickTimeout);
                _this.clickTimeout = setTimeout(function(){
                    console.log("handling click");
                    _this.onClick(e);
                },200);
                console.log(_this.clickTimeout);
            });
    
            node.on('dblclick', function (e) {
    
                console.log("cleared timeout in dblclick",_this.clickTimeout);
                clearTimeout(_this.clickTimeout);
                // Rest of the handler function
    
    0 讨论(0)
  • 2020-12-02 20:48

    I know this is old as heck, but thought I'd post anyhow since I just ran into the same problem. Here's how I resolved it.

     $('#alerts-display, #object-display').on('click', ['.item-data-summary', '.item-marker'], function(e) {
        e.preventDefault();
    
        var id;
    
        id = setTimeout(() => {
           // code to run here
           return false;
        }, 150);
    
        timeoutIDForDoubleClick.push(id);
    });
    
    
    $('.panel-items-set-marker-view').on('dblclick', ['.summary', '.marker'], function(e) {
        for (let i = 0; i < timeoutIDForDoubleClick.length; i++) {
           clearTimeout(timeoutIDForDoubleClick[i]);
        }
    
        // code to run on double click
    
        e.preventDefault();
    });
    
    0 讨论(0)
提交回复
热议问题