Skip to content

Instantly share code, notes, and snippets.

@EdoardoVignati
Created September 28, 2020 22:41
Show Gist options
  • Select an option

  • Save EdoardoVignati/82dcd56afcb76213f22f362ea9e502bb to your computer and use it in GitHub Desktop.

Select an option

Save EdoardoVignati/82dcd56afcb76213f22f362ea9e502bb to your computer and use it in GitHub Desktop.
Catch and distinguish between click and long press in jquery
<script>
//Checkout my improvement on StackOverflow: https://stackoverflow.com/a/34699164/6303265
var tmr = 0;
var islong = 0;
$(element).mousedown(function () {
tmr = setTimeout(function () {
// Handle the long-press
alert("You clicked for 1 second!");
console.log("You clicked for 1 second!");
islong=1;
}, 1000);
}).mouseup(function () {
if(islong == 0){
// Handle the click
alert("This is a click!");
console.log("This is a click!");
}
islong=0;
clearTimeout(tmr);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment