Created
February 6, 2019 22:02
-
-
Save miladvafaeifard/a8d0f0ea9a86b7e1abb0d245c2aea465 to your computer and use it in GitHub Desktop.
Throttle function in javascript with the example
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
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