Skip to main content
0:06est. 5 min

Debounce

// https://docs.google.com/document/d/1hvEjK5ZXMPYOr9HFoeCD4FM4b2dw-XsCwyB45M6Sz8Y/edit
// debounce: Grouping a sudden burst of events (like keystrokes) into a single one.

const deb = (fn, time) => {
let timeout = null;
return function () {
clearTimeout(timeout)
timeout = setTimeout(() => {
fn.apply(this, arguments)
}, time)
}
}

deb(search, 100);


const debounce = (fn, time) => {
let timeout = null
return function () {
clearTimeout(timeout)
timeout = setTimeout(() => {
fn.apply(this, arguments)
}, time);
}
}

function sum(x) {
console.log(x+2)
}

let doSum = sum(1);

let debounceSum = debounce(doSum, 200)

for (let i = 0; i < 10; i++) {
debouncedSum()
}

const greet = () => console.log('Hello World!')
const debouncedGreet = debounce(greet, 3000)

for (let i = 0; i < 10; i++) {
debouncedGreet()
}

// console.log(debounce(doSum, 200))

const debounce = (fn, delay) => {
let timer = null
return function () {
clearTimeout(timer);
timer = setTimeout(( ) => {
fn.apply(this, arguments);
}, delay);
}
}