Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created February 6, 2019 22:02
Show Gist options
  • Save miladvafaeifard/a8d0f0ea9a86b7e1abb0d245c2aea465 to your computer and use it in GitHub Desktop.
Save miladvafaeifard/a8d0f0ea9a86b7e1abb0d245c2aea465 to your computer and use it in GitHub Desktop.
Throttle function in javascript with the example
function throttle (func, delay, watch) {
watch && clearTimeout(watch);
return setTimeout(func, delay)
}
function callSearchApi(v) {
console.log(v);
}
let timeout = null;
const onUserType = value => {
timeout = throttle(
() => callSearchApi(value), 300, timeout
)
}
const inpt = document.getElementById('userInput');
inpt.addEventListener('keyup', e => {
onUserType(e.target.value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment