Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Last active September 7, 2022 08:21
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 kobus1998/b6a361eb0a29e16507c1e65e8e39bea1 to your computer and use it in GitHub Desktop.
Save kobus1998/b6a361eb0a29e16507c1e65e8e39bea1 to your computer and use it in GitHub Desktop.
debounce
function doAThing(a) {
console.log('aaaa', a);
}
function doBThing(a, b) {
console.log('bbb', a, b);
}
// var timeoutdebounce
/**
* the debounce callback takes in account the event parameter from the listener
*
* @param {callable} callback method executed once timeout finishes
* @param {int} delay amount of milliseconds before timoeut finishes
* @returns callable
*/
function debounce(callback, delay) {
if (typeof delay === 'undefined') {
delay = 300
}
return (function (...e) {
var exec = function () {
callback.timeoutdebounce = null
callback(...e)
}
clearTimeout(callback.timeoutdebounce)
callback.timeoutdebounce = setTimeout(exec, delay)
})
}
debounce(doAThing, 300)(1)
debounce(doBThing, 300)(2, 3)
debounce(doAThing, 300)(3)
debounce(doBThing, 300)(4, 5)
debounce(doAThing, 300)(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment