Created
June 18, 2020 14:52
-
-
Save codedrops-io/7d08931e03b44cdb5909cf8e4fc1184c to your computer and use it in GitHub Desktop.
Debounce function.
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 (func, wait = 100, immediate) { | |
let timeout | |
return function () { | |
const context = this | |
const args = arguments | |
const later = function () { | |
timeout = null | |
if (!immediate) func.apply(context, args) | |
} | |
const callNow = immediate && !timeout | |
clearTimeout(timeout) | |
timeout = setTimeout(later, wait) | |
if (callNow) func.apply(context, args) | |
} | |
} | |
export default debounce |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment