如何在jquery中计算mousedown事件期间的时间?
我正在尝试在鼠标关闭事件的一个按钮上执行两种不同的功能。 但它不起作用,因为我无法在mousedown事件中检测到时间如何在jquery中计算mousedown事件期间的时间?,jquery,jquery-ui,Jquery,Jquery Ui,我正在尝试在鼠标关闭事件的一个按钮上执行两种不同的功能。 但它不起作用,因为我无法在mousedown事件中检测到时间 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
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;
});
没有“jQuery”魔力,只有JavaScript计时器
var pressTimer
$("a").mouseup(function(){
clearTimeout(pressTimer)
// Clear timeout
return false;
}).mousedown(function(){
// Set timeout
pressTimer = window.setTimeout(function() { ... your code ...},1000)
return false;
});
以下是您可以使用的ref链接:
$(window).mousedown(function(e) {
clearTimeout(this.downTimer);
this.downTimer = setTimeout(function() {
alert('mousedown > 2 sec');
}, 2000);
}).mouseup(function(e) {
clearTimeout(this.downTimer);
});
小提琴:
参考资料:
谢谢您的快速回复。现在很好用。我不知道mousedown事件是否有downTimer变量。请提供我,如果任何更多的详细信息,停机时间变量以及上行时间变量,如果可用。还提供给我的编码使用behing downTimer变量。。。
var flag, flag2;
$("#ClikerButton").on({
mousedown : function(){
flag = new Date().getTime();
},
mouseup: function(){
flag2 = new Date().getTime();
var passed = flag2 - flag;
console.log(passed); //time passed in milliseconds
}
});