Created
May 15, 2018 14:27
-
-
Save vpiskunov/35af4f023e2c8a27c706560b66dba382 to your computer and use it in GitHub Desktop.
This file contains 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
<html> | |
<head> | |
<title>Debounce</title> | |
</head> | |
<body> | |
<button id="rocketButton" onclick="rocket.toggle()">Launch that rocket!</button> | |
<script> | |
let timeout = null; | |
let rocket = { | |
launched: false, | |
events: [], | |
toggle () { | |
clearTimeout(window.timeoutVar) | |
rocket.events.push( { type: rocket.launched ? 'land':'launch' } ); | |
const button = document.getElementById('rocketButton'); | |
button.innerHTML = 'Pending launch... cancel/land here'; | |
const rocketEvent = rocket.events.pop(); | |
rocket.launched = rocketEvent.type == 'land' ? false : true; | |
window.timeoutVar = setTimeout(() => { | |
const eventType = rocket.launched ? 'launched' : 'landed'; | |
// reset events collection | |
rocket.events = []; | |
const button = document.getElementById('rocketButton'); | |
button.innerHTML = rocket.launched ? "Land the rocket!" : "Launch that rocket again!" | |
window.alert(`Rocket has been ${eventType}! 🚀`); | |
}, 2000); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment