Last active
January 18, 2018 22:58
-
-
Save c0depanda/c7d4f6f61a6d3d6770d01a00ad8d4b90 to your computer and use it in GitHub Desktop.
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
// 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