Home > Article > Web Front-end > js imperfectly solves the conflict between click and dblclick events_javascript skills
Situation description
When an element, such as a div, is bound to both click and dblclick events, and these two events have to handle relatively independent businesses, that is, dblclick cannot be triggered when click, and dblclick cannot be triggered. trigger click. In the actual test, it was found that when dblclick, there will always be 1 click. It is this problem that will be solved below.
Situation Analysis
First of all, we need to understand the execution order of click and dblclick. The test process is abbreviated. The following are the test results:
Click: mousedown -- mouseup -- click
dblclick: mousedown -- mouseup - - click -- mousedown -- mouseup -- click -- dblclick
From this point of view, before dblclick is triggered, 2 clicks are actually executed, and the first click will be blocked (why? Well, I don’t know either).
Solution
The first thing that comes to mind is whether the event can be stopped, but I found that the browser does not provide a corresponding method. It is too difficult to implement it yourself, because the behavior associated with the click event must be made to be cancelable. That's all.
So I considered using delay, which was the only solution I could think of. Use setTimeout() to delay the completion of click event processing, and then use clearTimeout() to stop when you need to block the click.
Specific code