Created
March 29, 2019 21:36
-
-
Save jpenney1/99c8810876fdb84f52b0f3623c86b048 to your computer and use it in GitHub Desktop.
Run this code in an html script with an index element in the body, type anything into the input and see an alert after two seconds of inactivity
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
const debounce = (callback, wait) => { | |
let timeout = null | |
return (...args) => { | |
const next = () => callback(...args) | |
clearTimeout(timeout) | |
timeout = setTimeout(next, wait) | |
} | |
} | |
const inputEl = document.getElementsByTagName('input')[0] | |
inputEl.addEventListener('input', debounce(e => alert(`value updated: ${ e.target.value }`), 2000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment