Skip to content

Instantly share code, notes, and snippets.

@prograhammer
Created March 14, 2017 10:33
Show Gist options
  • Save prograhammer/329a85d9ee46b48f9ba5dcd9680ad025 to your computer and use it in GitHub Desktop.
Save prograhammer/329a85d9ee46b48f9ba5dcd9680ad025 to your computer and use it in GitHub Desktop.
// 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