开发者

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 we开发者_开发技巧bapp.

Thank you for your help!


$('#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.


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.


You can check the time to identify Click or Long Press [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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜