Created
December 14, 2024 00:58
-
-
Save zazaulola/bfaab341e411f233f259ea1db255bf0a to your computer and use it in GitHub Desktop.
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 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