如何在jquery的mousedown事件期间计算时间? [英] how to calculate time during mousedown event in jquery?

查看:134
本文介绍了如何在jquery的mousedown事件期间计算时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在mousedown上的一个按钮上执行2种不同的功能-向上事件. 但是它不起作用,因为我无法检测到mousedown事件的时间.

I am trying to do 2 different functionalities on one button on mousedown - up events. But its not working as i cant able to detect time on mousedown event.

var flag;  
$("#ClikerButton").mousedown(function(e){  
    //if mouse pressed more than 2 sec, then  
    // do func1 and set flag = true;  
         //else do nothing  
}).mouseup(function(e){  
//if flag == true, do nothing,  
//else do func2;  
});  

推荐答案

您可以使用:

$(window).mousedown(function(e) {
    clearTimeout(this.downTimer);
    this.downTimer = setTimeout(function() {
        alert('mousedown > 2 sec');   
    }, 2000); 
}).mouseup(function(e) {
    clearTimeout(this.downTimer); 
});​

提琴手: http://jsfiddle.net/simevidas/Pe9sq /2/

参考:检测鼠标按下事件的时间

这篇关于如何在jquery的mousedown事件期间计算时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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