How to calculate time during mousedown event in jquery?

I try to make two different functions on one button on events related to fraud. But it does not work, because I can not determine the time on the 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; }); 
+7
source share
3 answers

You can use:

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

Fiddle: http://jsfiddle.net/simevidas/Pe9sq/2/

Link: mousedown event detection time

+6
source

There is no "jQuery" magic, just JavaScript timers.

 var pressTimer $("a").mouseup(function(){ clearTimeout(pressTimer) // Clear timeout return false; }).mousedown(function(){ // Set timeout pressTimer = window.setTimeout(function() { ... your code ...},1000) return false; }); 

here's the ref link https://stackoverflow.com/a/4646263

+2
source
 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 } });​ 

Fiddle

+1
source

All Articles