Last active
November 17, 2019 08:16
-
-
Save intrnl/b3097b7d6d3062f24e07290850e0b630 to your computer and use it in GitHub Desktop.
Function debouncing
This file contains 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, 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