Add debounce function

This commit is contained in:
Jonathan Tran
2024-12-03 17:25:00 -05:00
parent 5178a72c52
commit fe83cd94ca

View File

@ -88,6 +88,29 @@ export function normaliseAngle(angle: number): number {
return result > 180 ? result - 360 : result
}
/**
* Returns a function that will delay the execution of the given function each
* time it's called until the wait time has passed.
*/
export function debounce<
F extends (...args: any[]) => void,
P extends Parameters<F>
>(func: F, wait: number): (...args: P) => void {
let timeout: ReturnType<typeof setTimeout> | null = null
function debounced(...args: P) {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
timeout = null
func(args)
}, wait)
}
return debounced
}
export function throttle<T>(
func: (args: T) => any,
wait: number