Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate time during mousedown event in jquery?

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;  
});  
like image 274
Jitendra Ahire Avatar asked May 02 '12 06:05

Jitendra Ahire


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/

Reference: Detecting time on mousedown event

like image 132
AlphaMale Avatar answered Sep 19 '22 01:09

AlphaMale


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 is the ref link https://stackoverflow.com/questions/2625210/long-press-in-javascript/2625240#2625240

like image 27
chhameed Avatar answered Sep 18 '22 01:09

chhameed


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

like image 29
adeneo Avatar answered Sep 22 '22 01:09

adeneo