Skip to content

Instantly share code, notes, and snippets.

@c-kick
Last active May 12, 2023 12:03
Show Gist options
  • Save c-kick/942ce38822c42ed4780ec264ca95b630 to your computer and use it in GitHub Desktop.
Save c-kick/942ce38822c42ed4780ec264ca95b630 to your computer and use it in GitHub Desktop.
doubleClickHandler - handles double-click events on elements
/**
* Handles the click event to detect single or double clicks.
* @param {MouseEvent} event - The click event object.
*/
function doubleClickHandler(event) {
const target = event.target;
if (new Date().getTime() - (target._lastTouch || 0) > 500) {
// Not a double click
target._lastTouch = new Date().getTime();
event.preventDefault();
} else {
// A double click
// Default event will proceed, do additional stuff here
}
}
// Attach the click event listener to the element with the ID 'doubleclick'
document.getElementById('doubleclick').addEventListener('click', doubleClickHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment