Skip to content

Instantly share code, notes, and snippets.

@jpenney1
Created March 29, 2019 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpenney1/99c8810876fdb84f52b0f3623c86b048 to your computer and use it in GitHub Desktop.
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
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