Get duration touch in javascript

Is it possible to get a touch duration using javascript on ios devices (and others, if possible)? I would like to distinguish between a long click and a short click in a web application for iphone.

Thanks for the help!

+4
source share
3 answers
$('#element').each(function() {

   var timeout,
       longtouch;

   $(this).bind('touchstart', function() {
      timeout = setTimeout(function() {
          longtouch = true;
      }, 1000);
   }).bind('touchend', function() {
       if (longtouch) {
          // It was a long touch.
       }
       longtouch = false;
       clearTimeout(timeout);
   });

});

jsFiddle .

The violin works with a long click.

+18
source

Take a look at jQuery mobile http://jquerymobile.com/demos/1.0a4.1/#docs/api/events.html

It has taps and events that sound like they can fit your needs.

+3
source

You can check Click or Long Press authentication time [jQuery]

function AddButtonEventListener() {
try {
var mousedowntime;
var presstime;
$("button[id$='" + buttonID + "']").mousedown(function() {
    var d = new Date();
    mousedowntime = d.getTime();
});
$("button[id$='" + buttonID + "']").mouseup(function() {
    var d = new Date();
    presstime = d.getTime() - mousedowntime;
    if (presstime > 999/*You can decide the time*/) {
        //Do_Action_Long_Press_Event();
    }
    else {
        //Do_Action_Click_Event();
    }
});
}
catch (err) {
alert(err.message);
}
} 
0
source

All Articles