kopia lustrzana https://github.com/Tldraw/Tldraw
20 wiersze
329 B
TypeScript
20 wiersze
329 B
TypeScript
export function throttle(fn: () => void, limit: number) {
|
|
let waiting = false
|
|
let invokeOnTail = false
|
|
return () => {
|
|
if (!waiting) {
|
|
fn()
|
|
waiting = true
|
|
setTimeout(() => {
|
|
waiting = false
|
|
if (invokeOnTail) {
|
|
invokeOnTail = false
|
|
fn()
|
|
}
|
|
}, limit)
|
|
} else {
|
|
invokeOnTail = true
|
|
}
|
|
}
|
|
}
|