如何在维持点击n秒后触发事件? - jQuery / Javascript [英] How to fire an event after n seconds of maintained click? - jQuery/Javascript

查看:118
本文介绍了如何在维持点击n秒后触发事件? - jQuery / Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我不是第一个找到这个,但我找不到解决问题的方法..

I am sure I am not the first looking for this, but I did not find any solution to my problem..

我正在寻找一种解雇方法只有在3秒钟后才能点击一个事件。
我尝试使用带有mouseup / mousedown Jquery事件的javascript setInterval()函数,但它没有用。

I am looking for a way to fire an event after and only after a 3 seconds maintained click. I tried with javascript setInterval() function with mouseup/mousedown Jquery events but it did't work.

有人有想法吗?

我有一个div,我按住鼠标按钮3秒钟,然后会触发一些东西。每次
3秒计时器必须重新初始化。

I have a div, I keep the mouse button down for 3 seconds, and something will be fired. 3 seconds timer must be reinitialized every time.

推荐答案

致电 setTimeout()在3000毫秒后执行操作,将来自 setTimeout()的标识符存储到作用于函数上方的变量中。在元素的 mouseup()上,如果存在超时,则通过 clearTimeout()清除超时。

Call setTimeout() to perform your action after 3000 milliseconds, storing the identifier from setTimeout() into a variable scoped above the function. On the element's mouseup(), clear the timeout if it exists via clearTimeout().

var divMouseDown;
$('#div-id').mousedown(function() {
  divMouseDown = setTimeout(function() {
     // Do timeout action...
  }, 3000);
});
$('#div-id').mouseup(function() {
  if (divMouseDown) {
    clearTimeout(divMouseDown);
  }
});

这篇关于如何在维持点击n秒后触发事件? - jQuery / Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆