JS is not perfect to solve the click and DblClick event conflict problem _javascript Skills

Source: Internet
Author: User
Situation description
When an element, such as a Div, binds both the Click event and the DblClick event, and the two events are handled in a relatively independent business, that is, the click cannot trigger the Dblclick,dblclick when it is not triggered. In the actual test found that when the DblClick, there will always be 1 times click. This is the problem that will be addressed below.
Situation Analysis
First we need to be clear about the order of execution of Click and DblClick, the test procedure is slightly, the following is the test result:
In this view, before the DblClick trigger, actually executes 2 times click, and the first click will be blocked (why?) Well, I don't know either.
The first thought is whether you can stop the event, but find that the browser does not provide the appropriate method, if it is too difficult to implement, because click the event associated with the behavior must be made can be cancel the line.
So consider the delay, is the only solution I can think of, using settimeout () to delay the completion of the Click event, and then use Cleartimeout () to stop when you need to screen click.
Specific code
Copy Code code as follows:

var test = (function () {
var clicktext = ' click<br> ';
var dblclicktext = ' dblclick<br> ';
var timer = null;
return {
Click:function () {
Cleartimeout (timer);
Timer = settimeout (function () {
$ (' body '). Append (Clicktext);
}, 300);
Dblclick:function () {
Cleartimeout (timer);
$ (' body '). Append (Dblclicktext);
Init:function () {
$ (function () {
$ (' div '). Click (Test.click). DblClick (Test.dblclick);
Test.init ();

HTML code
Copy Code code as follows:

<div style= "Width:100px;height:100px;background:red;text-align:center;line-height:33px;-moz-user-select:none ;-khtml-user-select:none;user-select:none ">click<br>or<br>dblclick</div>

The article title said, is not perfect, because Windows, Control Panel can be adjusted mouse double-click speed (other systems are not clear), so I set the system set the double-click speed is slow, then the above demo will not take effect. So 300 milliseconds is just a ballpark.
Author: Hu Yuan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.