Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the duration of a touch in javascript

Is it possible to get the duration of a touch using javascript on ios devices (and others if possible)? I would like to differentiate a long press from a short "click" press on an iphone webapp.

Thank you for your help!

like image 516
cmplieger Avatar asked May 18 '11 00:05

cmplieger


2 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 fiddle works with a long click.

like image 104
alex Avatar answered Sep 19 '22 14:09

alex


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

It has tap, and taphold events, which sounds like it might fit your needs.

like image 30
Alxandr Avatar answered Sep 18 '22 14:09

Alxandr