Skip to content

Instantly share code, notes, and snippets.

@mohitsharma93
Last active September 26, 2022 16:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
/**
* 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