javascript 在javascript中获取触摸的持续时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6038613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:14:53  来源:igfitidea点击:

Get the duration of a touch in javascript

javascriptjqueryiphonetimertouch

提问by cmplieger

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.

是否可以在 ios 设备(以及其他设备,如果可能)上使用 javascript 获得触摸的持续时间?我想在 iphone webapp 上区分长按和短按“点击”。

Thank you for your help!

谢谢您的帮助!

回答by alex

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

js小提琴

The fiddle works with a long click.

长按小提琴即可

回答by Alxandr

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

看看 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.

它有 tap 和 taphold 事件,听起来它可能适合您的需求。

回答by Derin

You can check the time to identify Click or Long Press [jQuery]

可以通过查看时间来识别 Click 或 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);
}
}