Skip to content

Instantly share code, notes, and snippets.

@c-kick
Last active May 12, 2023 12:03
Embed
What would you like to do?
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