Skip to content

Instantly share code, notes, and snippets.

@mersanuzun
Last active June 25, 2018 16:31
Show Gist options
  • Save mersanuzun/7c659e5a975f144f93b75b9b26984619 to your computer and use it in GitHub Desktop.
Save mersanuzun/7c659e5a975f144f93b75b9b26984619 to your computer and use it in GitHub Desktop.
Debounce example: It runs the passing function after keyup event is triggred and 500 milis is passed.
function debounce(event, func) {
const target = event.target;
clearTimeout(target.setTimeoutId);
target.setTimeoutId = setTimeout(() => func(target.value), 500);
}
document.getElementById("name")
.addEventListener("keyup", (event) => {
debounce(event, (value) => {
console.log("Print it after 500ms delay. Value: ", value);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment