how to calculate time during mousedown event in jquery?

jquery, jquery-ui

Solution

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

Problem

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; }); ```

Original source

Related problems