Created
November 19, 2018 17:15
-
-
Save Opus1no2/80d763a236354b49fde240cf588dfb05 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
<!doctype html> | |
<html lang='en'> | |
<head> | |
<meta charset='utf-8'> | |
<style> | |
.body { | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<input type='range' name='prog' value='5000' min='0' max='10000'> | |
<div data-for='prog'></div> | |
<script> | |
const progVal = document.querySelector('[data-for="prog"]'); | |
const progInput = document.querySelector('[name="prog"]'); | |
const debounced = debounce(inProgress, 500, true); | |
progVal.innerHTML = progInput.value; | |
progInput.addEventListener('change', (e) => { | |
progVal.innerHTML = progInput.value; | |
}); | |
progInput.addEventListener('input', debounced); | |
function inProgress() { | |
progVal.innerHTML = 'updating...'; | |
} | |
function debounce(func, wait, immediate) { | |
let timeout; | |
return () => { | |
const context = this, args = arguments; | |
const later = function() { | |
timeout = null; | |
if (!immediate) func.apply(context, args); | |
}; | |
const callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
timeout = setTimeout(later, wait); | |
if (callNow) func.apply(context, args); | |
}; | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment