Function: useTimeout()
@arolariu/components / useTimeout
Function: useTimeout()
useTimeout(
callback,delay):void
Defined in: hooks/useTimeout.tsx:31
Executes a callback after a specified delay with automatic cleanup.
Parameters
callback
() => void
The function to execute after the delay.
delay
number
The delay in milliseconds, or null to disable the timeout.
Returns
void
Remarks
This hook wraps setTimeout and automatically clears the timeout when the component
unmounts or when the delay changes. Setting delay to null disables the timeout.
The timeout is reset whenever the callback or delay changes, ensuring the most
recent callback is always executed.
Example
function DelayedMessage() {
const [visible, setVisible] = useState(false);
useTimeout(() => {
setVisible(true);
}, 3000);
return visible ? <p>Message appeared!</p> : <p>Waiting...</p>;
}
// was this page useful?