JS Debounce Explained
Ning Kuang
1 min read
store
timeout
about a callback, initial value isnull
return a function wrapping the original callback
when
timeout
isnull
, clear the timeoutthen set a timer to execute callback with the set
delay
in ms, and store the timer id to variabletimeout
function debounce(callback, delay) {
let timeout = null
return (...args) => {
if (timeout !== null) clearTimeout(timeout)
timeout = setTimeout(() => {
callback.apply(this, args)
}, delay)
}
}
0
Subscribe to my newsletter
Read articles from Ning Kuang directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by