Created
February 11, 2020 14:05
-
-
Save motsu0/c35589f34a602b3822fc41d4108ccaca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function long_press(el,nf,lf,sec){ | |
let longclick = false; | |
let longtap = false; | |
let touch = false; | |
let timer; | |
el.addEventListener('touchstart',()=>{ | |
touch = true; | |
longtap = false; | |
timer = setTimeout(() => { | |
longtap = true; | |
lf(); | |
}, sec); | |
}) | |
el.addEventListener('touchend',()=>{ | |
if(!longtap){ | |
clearTimeout(timer); | |
nf(); | |
}else{ | |
touch = false; | |
} | |
}) | |
el.addEventListener('mousedown',()=>{ | |
if(touch) return; | |
longclick = false; | |
timer = setTimeout(() => { | |
longclick = true; | |
lf(); | |
}, sec); | |
}) | |
el.addEventListener('click',()=>{ | |
if(touch){ | |
touch = false; | |
return; | |
} | |
if(!longclick){ | |
clearTimeout(timer); | |
nf(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment