如何在同一元素上设置单击和双击的功能?

huangapple go评论68阅读模式
英文:

How to put a function for click and another for double click on the same element?

问题

我试图为一个元素实现两种不同的功能,一个是单击后,另一个是双击后。我正在使用setTimeout来检测单击事件,但这种方式会导致两个功能都被执行,因为setTimeout会继续计时。所以我想到了在第二次点击后停止计时,但之后setTimeout停止工作,一切都被视为双击。所以我将代码模型化,直到看起来像这样:

function howManyClicks(element){
    var time;
    let realization = new Promise((resolve)=>{
        time = setTimeout(()=>{
            resolve(1);
            element.removeEventListener('click', doubleClick);
        }, 200);
        element.addEventListener('click', doubleClick);
        function doubleClick(){
            element.removeEventListener('click', doubleClick);
            clearTimeout(time);
            resolve(2);
        }
    });
    realization.then((value)=>{
        return value;
    });
}

我认为已经有足够多的非功能性和不必要的东西在这里,但它们是我为了达到目标而尝试的。有谁知道如何解决这个问题吗?

英文:

I'm trying to implement two different functionalities to an element, one after a single click and another one after two clicks. I'm using a setTimeout to detect when a single click is performed, but this way both functions end up being performed, because the setTimeout continues counting, so I had the idea of ​​stopping the time as soon as the second click is performed, but after that the setTimeout stops working and everything is seen as a double click, so I modeled everything until it looked like this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function howManyClicks(element){
	var time;
	let realization = new Promise((resolve)=&gt;{
		time = setTimeout(()=&gt;{
			resolve(1);
			element.removeEventListener(&#39;click&#39;, dobleClick);
		}, 200);
		element.addEventListener(&#39;click&#39;, dobleClick);
		function dobleClick(){
			element.removeEventListener(&#39;click&#39;, dobleClick);
			clearTimeout(time);
			resolve(2);
		}
	});
	realization.then((value)=&gt;{
		return value;
	});
}

<!-- end snippet -->

I think there are already enough non-functional and unnecessary things there, but they were my attempts to reach that goal. Does anyone know how to resolve?

答案1

得分: 1

除了我们的朋友指出已经存在一个双击事件的事实之外...

几乎总是在双击触发之前触发单击,因为大多数用户无法完美同步两个按钮以防止左按钮触发,所以这是一个不好的主意,几乎没有人使用双击事件的原因。

因此,对于没有冲突的双鼠标按钮功能,最好的解决方案是使用左右鼠标按钮...

element.addEventListener('click', function_A); // 左击

element.addEventListener('contextmenu', function_B); // 右击

虽然我不知道右键或双击在Macintosh上如何工作,因为我从未使用过! 如何在同一元素上设置单击和双击的功能?

英文:

Other than the fact that there already exists a double click event which our friend above pointed out...

A double click will almost always trigger a single click prior to a double click firing, because most users out there can't perfectly synchronize the two buttons to prevent the left button from firing, so it's a bad idea and the reason next to nobody uses the dblclick event.

Therefore the best solution for dual mouse button functions with no conflicts, is to use the left and right mouse buttons...

 element.addEventListener(&#39;click&#39;,function_A);  // left click

 element.addEventListener(&#39;contextmenu&#39;,function_B);  // right click

Although I have no idea how the right or double clicks would work on a Macintosh having never used one! 如何在同一元素上设置单击和双击的功能?

答案2

得分: 0

var clicks = 0;
function func(element){
  clicks++;
  let time = setTimeout(() => {
    if(clicks == 1){
      element.innerHTML = 'One click';         
      clicks = 0;
    }
  }, 200);
  if(clicks == 2){
    element.innerHTML = 'Two clicks';  
    clearTimeout(time);
    clicks = 0;
  }
}
<button onclick="func(this)">Button</button>

Enjoy!!!


<details>
<summary>英文:</summary>



&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    var clicks = 0;
    function func(element){
      clicks++;
      let time = setTimeout(()=&gt;{
        if(clicks == 1){
          element.innerHTML = &#39;One click&#39;;         
          clicks = 0;
        }
      },200);
      if(clicks == 2){
        element.innerHTML = &#39;Two clicks&#39;;  
        clearTimeout(time);
        clicks = 0;
      }
    }

&lt;!-- language: lang-html --&gt;

    &lt;button onclick=&quot;func(this)&quot;&gt;Button&lt;/button&gt;

&lt;!-- end snippet --&gt;

Enjoy!!!

</details>



huangapple
  • 本文由 发表于 2023年6月12日 04:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定