Skip to content
Advertisement

Prevent click event from firing when dblclick event fires

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 to firing the double click event, the click event is also fired twice. What is the best approach for preventing this behavior?

Advertisement

Answer

In a comment, you said,

I delay the click handler by 300 ms (a noticeable and annoying delay) and even …

So it sounds like what you want is that when you click then the DOM should geneate a click event immediately, except not if the click is the first click of a double-click.

To implement this feature, when you click, the DOM would need to be able to predict whether this is the final click or whether it’s the first of a double-click (however I don’t think is possible in general for the DOM to predict whether the user is about to click again).


What are the two distinct actions which you’re trying to take on click and double-click? IMO, in a normal application you might want both events: e.g. single-click to focus on an element and then double-click to activate it.

When you must separate the events, some applications use something other than double-click: for example, they use right-click, or control-click.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement