Christophe Debove Christophe Debove - 5 days ago 5
Android Question

How to detect a long touch pressure with javascript for android and iphone?

How to detect a long touch pressure with javascript for android and iphone?
native javascript or jquery...

I want something that sound like :

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

Answer

The problem with using Touch End to detect the long touch is it won't work if you want the event to fire after a certain period of time. It is better to use a timer on touch start and clear the event timer on touch end. The following pattern can be used:

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 };