Created
May 13, 2020 22:16
-
-
Save DRFR0ST/f1d3d643ccb61fe02aefd81c316e0ca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a function that delays callback. | |
*/ | |
const useDelay = () => { | |
const ref = useRef<(fn: Function, ms: number) => void | null>(null); | |
if(ref.current === null) { | |
let t = -1; | |
const delay = (fn: Function, ms: number) => { | |
clearTimeout(t); | |
t = setTimeout(fn, ms); | |
} | |
ref.current = delay; | |
} | |
return ref.current as (fn: Function, ms: number) => void; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment