export function debounce(callback, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => {
callback.apply(this, args);
}, delay);
};
}
function sayHello(num) {
console.log(num);
}
const debounced = debounce(sayHello, 200);
debounced(1);
// debounced(2)
// debounced(3)
// debounced(4)
// debounced(5)
// setTimeout(() => {
// debounced(6)
// }, 1000)
Click Run to execute JavaScript.
Code runs in an isolated environment.
Code runs in an isolated environment.