Created
March 14, 2017 10:33
-
-
Save prograhammer/329a85d9ee46b48f9ba5dcd9680ad025 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
// Returns a function, that, as long as it continues to be invoked, will not | |
// be triggered. The function will be called after it stops being called for | |
// N milliseconds. If `execAsap` is passed, trigger the function on the | |
// leading edge, instead of the trailing. | |
export function debounce (func, threshold, execAsap) { | |
var timeout | |
return function debounced () { | |
var obj = this | |
var args = arguments | |
function delayed () { | |
if (!execAsap) func.apply(obj, args) | |
timeout = null | |
} | |
if (timeout) clearTimeout(timeout) | |
else if (execAsap) func.apply(obj, args) | |
timeout = setTimeout(delayed, threshold || 100) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment