2627. Debounce
https://leetcode.com/problems/debounce/
Javascript
var debounce = function(fn, t) {
let timer
return function(...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => fn(...args), t)
}
};
Typescript
type F = (...p: any[]) => any
function debounce(fn: F, t: number): F {
let timer: ReturnType<typeof setTimeout> | undefined;
return function(...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => fn(...args), t)
}
}