使用JavaScript/jQuery的Mousedown计时器 [英] Mousedown Timer using JavaScript/jQuery

查看:85
本文介绍了使用JavaScript/jQuery的Mousedown计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道用户按下鼠标按钮多长时间(在网页上的任意位置)?当用户按住鼠标按钮至少2-3秒(最好在此过程中取消鼠标)时,我想执行一个功能.这可能吗?

How can I know how long a user has been holding the mouse button down (anywhere on a webpage)? I want to execute a function when the user held the mouse button for at least 2-3 seconds (preferably cancelling the mouse down in the process). Is this possible?

推荐答案

在这里:

$(window).mousedown(function(e) {
    clearTimeout(this.downTimer);
    this.downTimer = setTimeout(function() {
        // do your thing 
    }, 2000);
}).mouseup(function(e) {
    clearTimeout(this.downTimer);
});

实时演示: http://jsfiddle.net/simevidas/Pe9sq/2/

这篇关于使用JavaScript/jQuery的Mousedown计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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