JS Debounce Explained
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)
}
}