Prevent click event from firing when dblclick event fires

前端 未结 12 1251
名媛妹妹
名媛妹妹 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: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);
    }
    

提交回复
热议问题