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

后端 未结 8 1596
天涯浪人
天涯浪人 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:33

    We can calculate the time difference when the touch started and when the touch end. If the calculated time difference exceed the touch duration then we use a function name taphold.

    var touchduration = 300; 
    var timerInterval;
    
    function timer(interval) {
    
        interval--;
    
        if (interval >= 0) {
            timerInterval = setTimeout(function() {
                                timer(interval);
                            });
        } else {
            taphold();
        }
    
    }
    
    function touchstart() {
        timer(touchduration);
    }
    
    function touchend() {
        clearTimeout(timerInterval);
    }
    
    function taphold(){
        alert("taphold");
    }
    
    
    document.getElementById("xyz").addEventListener('touchstart',touchstart);
    document.getElementById("xyz").addEventListener('touchend',touchend);
    

提交回复
热议问题