mirror of
https://github.com/beilunyang/moemail.git
synced 2026-07-12 16:11:20 +08:00
feat: Init
This commit is contained in:
19
app/hooks/use-throttle.ts
Normal file
19
app/hooks/use-throttle.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useCallback, useRef } from 'react'
|
||||
|
||||
export function useThrottle<T extends (...args: any[]) => void>(
|
||||
fn: T,
|
||||
delay: number
|
||||
): T {
|
||||
const lastRun = useRef(Date.now())
|
||||
|
||||
return useCallback(
|
||||
((...args) => {
|
||||
const now = Date.now()
|
||||
if (now - lastRun.current >= delay) {
|
||||
fn(...args)
|
||||
lastRun.current = now
|
||||
}
|
||||
}) as T,
|
||||
[fn, delay]
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user