Last active
September 26, 2022 16:06
-
-
Save mohitsharma93/3a7cc835c5878a283a2538657c1e932d to your computer and use it in GitHub Desktop.
debounce is decorator wrapper that suspends calls to f until there’s ms milliseconds of inactivity
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
/** | |
* use this function if you want to call other function after some | |
* time(provided by you in ms parameter) with latest arguments. | |
* Basically it delay our functino call by given MS(milliseconds). | |
*/ | |
function debounce(fun,ms) { | |
let time; // storing timer returned by setTimeout api. | |
return function() { | |
clearTimeout(time); // clear timer on each time of function call. | |
time = setTimeout(() => fun(...arguments), ms) | |
} | |
} | |
function plus(a, b) { | |
console.log(a + b); | |
} | |
// calling plus function after 500ms | |
const check = debounce(pluc, 500) | |
check(5, 6) // we not see result in console bcz we call next statement without any delay | |
check(6, 7) // we see 13 in console as expected result, if you want to see |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment