Skip to content

Instantly share code, notes, and snippets.

@motsu0
Created February 11, 2020 14:05
Show Gist options
  • Save motsu0/c35589f34a602b3822fc41d4108ccaca to your computer and use it in GitHub Desktop.
Save motsu0/c35589f34a602b3822fc41d4108ccaca to your computer and use it in GitHub Desktop.
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