javascript tap and hold

Solutions on MaxInterview for javascript tap and hold by the best coders in the world

showing results for - "javascript tap and hold"
Giorgio
10 Apr 2019
1var onlongtouch; 
2var timer;
3var touchduration = 800; //length of time we want the user to touch before we do something
4
5function touchstart(e) {
6    e.preventDefault();
7    if (!timer) {
8        timer = setTimeout(onlongtouch, touchduration);
9    }
10}
11
12function touchend() {
13    //stops short touches from firing the event
14    if (timer) {
15        clearTimeout(timer);
16        timer = null;
17    }
18}
19
20onlongtouch = function() { 
21    timer = null;
22    document.getElementById('ping').innerText+='ping\n'; 
23};
24
25document.addEventListener("DOMContentLoaded", function(event) { 
26    window.addEventListener("touchstart", touchstart, false);
27    window.addEventListener("touchend", touchend, false);
28});