Created
September 28, 2020 22:41
-
-
Save EdoardoVignati/82dcd56afcb76213f22f362ea9e502bb to your computer and use it in GitHub Desktop.
Catch and distinguish between click and long press in jquery
This file contains hidden or 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
| <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