3

How to detect long press on mouseup events,Should not use any other mouse events(strictly)

$("#phone").find("button").mouseup(function(event){

I have this mouseup event,inside which I'm calling a function

Inside that function I need to detect longpress

How to achieve it?

Thanks

Naim FS
  • 61
  • 9
  • Check this: http://stackoverflow.com/a/2625240/3647441 - You cannot solve this problem by ony using the `mouseup` event because you have to recognise when the mouse button is pressed (`mousedown` event). – mario.van.zadel Sep 17 '15 at 05:34

2 Answers2

4

Keep Track of The Time Between mousedown and mouseup

Sounds as simple as it is, log the time when the user triggers the mousedown event, and when the user triggers the mouseup event. Subtract these two numbers and test to see if they are greater than your desired threshold

// Get the time of mouse down
var mouseDownTime;    
var threshold = 1000; // 1000 milliseconds == 1 second. 

$("#phone").find("button").mousedown(function(event){
    var mouseDownDate = new Date();
    mouseDownTime = mouseDownDate.getTime();
});

$("#phone").find("button").mouseup(function(event){
    var mouseUpDate = new Date();
    var mouseUpTime = mouseUpDate.getTime();
    if(mouseUpTime - mouseDownTime > threshold){
        // Code you want called after being triggered.
    }
});
Daymon Schroeder
  • 682
  • 1
  • 4
  • 23
1

try this

var timeOut;
$("button").mouseup(function(event){

    clearTimeout(timeOut);
});
$("button").mousedown(function(event){

    timeOut = setTimeout(function(){

    alert('you hold your mouse more than 2 seconds.!');

    },2000);
});

SEE DEMO

Syed Arif Iqbal
  • 1,830
  • 12
  • 16