Skip to content

Instantly share code, notes, and snippets.

@intrnl
Last active November 17, 2019 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intrnl/b3097b7d6d3062f24e07290850e0b630 to your computer and use it in GitHub Desktop.
Save intrnl/b3097b7d6d3062f24e07290850e0b630 to your computer and use it in GitHub Desktop.
Function debouncing
function debounce (fn, ms, immediate = false) {
let timeout;
return function () {
const call = () => {
timeout = null;
if (!immediate) fn.apply(this, arguments);
};
const now = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(call, ms);
if (now) fn.apply(this, arguments);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment