main
ts 23 lines 504 Bytes
Raw
1 export function debounce (callback: () => void, wait: number = 100): () => void {
2 let timeout: ReturnType<typeof setTimeout>
3 let start: number | undefined
4
5 return (): void => {
6 if (start == null) {
7 start = Date.now()
8 }
9
10 if (timeout != null && Date.now() - start > wait) {
11 clearTimeout(timeout)
12 start = undefined
13 callback()
14 return
15 }
16
17 clearTimeout(timeout)
18 timeout = setTimeout(() => {
19 start = undefined
20 callback()
21 }, wait)
22 }
23 }