Prevent click event from firing when dblclick event fires

前端 未结 12 1257
名媛妹妹
名媛妹妹 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:58

    Summarizing, to recognize the simpleClick and doubleClick events on the same element, just treat the onClick event with this method:

    var EVENT_DOUBLE_CLICK_DELAY = 220; // Adjust max delay btw two clicks (ms)
    var eventClickPending = 0;
    
    function onClick(e){
        if ((e.detail == 2 ) && (eventClickPending!= 0)) {
    //       console.log('double click action here ' + e.detail);
             clearTimeout(eventClickPending);
             eventClickPending = 0;
             // call your double click method
             fncEventDblclick(e);
    
        } else if ((e.detail === 1 ) && (eventClickPending== 0)){   
    //      console.log('sigle click action here 1');
            eventClickPending= setTimeout(function() {
    //          console.log('Executing sigle click');
                eventClickPending = 0
                // call your single click method
                fncEventClick(e);
            }, EVENT_DOUBLE_CLICK_DELAY);
    
    //    } else { // do nothing
    //      console.log('more than two clicks action here ' + e.detail);
                
        }
    }
    

提交回复
热议问题