Summary: This file was getting overwelhming, and this seemed like a reasonable way to split it Reviewed By: elboman Differential Revision: D47547532 fbshipit-source-id: ab2bfa22daabbed13ec1445da0cf8ba88bda12d7
31 lines
753 B
TypeScript
31 lines
753 B
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
import {useEffect, useRef, useState} from 'react';
|
|
|
|
export function useDelay(delayTimeMs: number) {
|
|
const [isDone, setIsDone] = useState(false);
|
|
const delayTimerStarted = useRef(false);
|
|
useEffect(() => {
|
|
let handle: NodeJS.Timeout | null = null;
|
|
if (delayTimerStarted.current === false) {
|
|
handle = setTimeout(() => setIsDone(true), delayTimeMs);
|
|
delayTimerStarted.current = true;
|
|
}
|
|
|
|
return () => {
|
|
if (handle !== null) {
|
|
clearTimeout(handle);
|
|
}
|
|
};
|
|
}, [delayTimeMs]);
|
|
|
|
return isDone;
|
|
}
|