Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Define variable
let pressTimer = null;
// Create timeout ( run function after 1s )
let start = (e) => {
if (e.type === 'click' && e.button !== 0) {
return;
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
// Execute something !!!
}, 1000);
}
}
// Cancel Timeout
let cancel = (e) => {
// Check if timer has a value or not
if (pressTimer !== null) {
clearTimeout(pressTimer);
pressTimer = null;
}
}
// select element with id longPressButton
let el = document.getElementById('longPressButton');
// Add Event listeners
el.addEventListener("mousedown", start);
// Cancel timeouts if this events happen
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment