Last active
September 7, 2022 08:21
-
-
Save kobus1998/b6a361eb0a29e16507c1e65e8e39bea1 to your computer and use it in GitHub Desktop.
debounce
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 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