Skip to content

Instantly share code, notes, and snippets.

@sanishkr
Last active February 3, 2020 10:40
Show Gist options
  • Save sanishkr/eac06aa9ee3dde9a8e2f3492ac6724f4 to your computer and use it in GitHub Desktop.
Save sanishkr/eac06aa9ee3dde9a8e2f3492ac6724f4 to your computer and use it in GitHub Desktop.
const debounced = (fn, delay) => {
let timerId = null;
return function() {
const args = arguments;
const ctx = this;
console.log("Trying....")
clearTimeout(timerId)
timerId = setTimeout(() => {
fn.apply(ctx, args)
}, delay)
}
}
// Example #1
const logger = debounced(() => console.log(new Date().toString()), 500);
for(let i = 0; i<10; i++){
setTimeout(logger, i*50)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment