小编典典

如何使用Android和iPhone的javascript检测长按压力?

html

如何使用Android和iPhone的javascript检测长按压力?本机javascript或jquery …

我想要听起来像:

<input type='button' onLongTouch='myFunc();' />

阅读 291

收藏
2020-05-10

共1个答案

小编典典

使用“触摸结束”检测长时间触摸的问题是,如果您希望事件在一定时间后触发,则它将无法正常工作。最好在触摸开始时使用计时器,并在触摸结束时清除事件计时器。可以使用以下模式:

var onlongtouch; 
var timer;
var touchduration = 500; //length of time we want the user to touch before we do something

touchstart() {
    timer = setTimeout(onlongtouch, touchduration); 
}

touchend() {

    //stops short touches from firing the event
    if (timer)
        clearTimeout(timer); // clearTimeout, not cleartimeout..
}

onlongtouch = function() { //do something };
2020-05-10