Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Created December 14, 2024 00:58
Show Gist options
  • Save zazaulola/bfaab341e411f233f259ea1db255bf0a to your computer and use it in GitHub Desktop.
Save zazaulola/bfaab341e411f233f259ea1db255bf0a to your computer and use it in GitHub Desktop.
function debounce(fn, ms, obj){
return Object.assign( function fx(...args){
const now = Date.now();
if(now - fx.lastCall > ms){
fx.lastCall = now;
return fx.fn.call(fx.obj,...args);
}
if(fx.timeout) clearTimeout(fx.timeout);
fx.timeout = setTimeout(()=>fx(...args), fx.lastCall + ms - now);
}, { fn, obj, lastCall: 0, timeout: null });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment