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

后端 未结 8 1660
天涯浪人
天涯浪人 2020-11-27 04:05

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

I want something that sound like :



        
8条回答
  •  执念已碎
    2020-11-27 04:40

    This better solution based on @Joshua, sometimes the code need to be called directly inside event (some web API require user acction to trigger something) for this case you can use this modification:

    var longtouch;
    var timer;
    //length of time we want the user to touch before we do something
    var touchduration = 500;
    
    function touchstart() {
        longtouch = false;
    
        timer = setTimeout(function() {
           longtouch = true;
           timer = null
        }, touchduration);
    }
    
    function touchend() {
        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
        if (longtouch) {
            // your long acction inside event
            longtouch = false;
        }
    }
    

    in setTimeout you set the flag to true and inside touchend, you check if it was set.

提交回复
热议问题