Debounce Function
NodeJS
Medium
6 views
Problem Description
Build a debounce(fn, ms) and show it only calls once.
Output Format
Print integer calls.
Constraints
Call debounced function many times quickly.
Official Solution
function debounce(fn, ms) {
let id;
return (...args) => {
clearTimeout(id);
id = setTimeout(() => fn(...args), ms);
};
}
let calls = 0;
const d = debounce(() => { calls += 1; console.log(calls); }, 30);
d();
d();
d();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!