Determining Long Tap (Long Press, Tap Hold) on Android with jQuery

前端 未结 5 718
一生所求
一生所求 2021-01-02 10:30

I\'ve been able to successfully play with the touchstart, touchmove, and touchend events on Android using jQuery and an HTML page. Now I\'m trying to see what the trick is t

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 11:16

    Why not just use a js timer? I did something like:

    i=0;
    $drink = document.getElementById('drink');
    $drink.addEventListener('touchstart',function(event){
        $('#drink .mainBtnText').text('HOLD'); //mostly for debugging
        t = window.setInterval(function(event){
                i+=.5;
                if (i>=1){
                    alert('taphold');
                    window.clearInterval(t);
                    i=0;
                }
            },500);
    });
    $drink.addEventListener('touchend',function(event){
        $('#drink .mainBtnText').text('Drink');
        i=0;
        window.clearInterval(t);
    });
    

    And I've had absolutely no trouble with it thus far...admittedly, I haven't done incredibly extensive device testing, but it's worked on my desktop (with mousedown/mouseup) as well as an HTC Droid Eris (Android 1.6) and my Droid RAZR (Android 2.3)...

提交回复
热议问题