Home > Web Front-end > JS Tutorial > body text

Resolve the conflict between double-click and click events

php中世界最好的语言
Release: 2018-06-09 10:47:03
Original
1830 people have browsed it

This time I will introduce to you how to resolve the conflict between double-click and click events. What are the precautions for resolving the conflict between double-click and click events? The following is a practical case, let's take a look.

In the same function block in the JS code, click and double-click events are usually used at the same time, but we usually encounter a problem, that is, a double-click event is executed when double-clicking, and it is also executed Double click event. Such conflicts are often encountered in ZTree and DHTMLX.

If you want to resolve the conflict between the two events, you need to delay the click event. If a click event is detected during this delay, then the two clicks are considered to be a double-click event, then only Execute the double-click event and clear the delay timer immediately to prevent the second click from taking effect.

The specific code is as follows:

var clickFlag = null;//是否点击标识(定时器编号)
function doOnClick(...) {
  if(clickFlag) {//取消上次延时未执行的方法
    clickFlag = clearTimeout(clickFlag);
  }
  
  clickFlag = setTimeout(function() {
    // click 事件的处理
  }, 300);//延时300毫秒执行
}
function doOnDblClick(...) {
  if(clickFlag) {//取消上次延时未执行的方法
    clickFlag = clearTimeout(clickFlag);
  }
  
  // dblclick 事件的处理
}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of select component case

##Encapsulate Vue2 routing navigation hook and use it in actual combat

The above is the detailed content of Resolve the conflict between double-click and click events. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template