Last active
February 3, 2020 10:40
-
-
Save sanishkr/eac06aa9ee3dde9a8e2f3492ac6724f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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