Skip to content

Instantly share code, notes, and snippets.

@DRFR0ST
Created May 13, 2020 22:16
Show Gist options
  • Save DRFR0ST/f1d3d643ccb61fe02aefd81c316e0ca0 to your computer and use it in GitHub Desktop.
Save DRFR0ST/f1d3d643ccb61fe02aefd81c316e0ca0 to your computer and use it in GitHub Desktop.
/**
* 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