Skip to main content
Version: 5.7.2

useTimeoutWhen

About​

Takes a callback and fires it when a condition is true

Installation​

npm install --save rooks

Importing the hook​

import { useTimeoutWhen } from "rooks";

Usage​

function Demo() {
const [start, setStart] = useState(false);
useTimeoutWhen(() => setStart(false), 2000, start);
return (
<>
<h1>Rooks: useTimeoutWhen example</h1>
<hr></hr>
<p>Click the button below to disable it for 2 seconds</p>
<button onClick={() => setStart(true)} disabled={start}>
Start timeout
</button>
</>
);
}

render(<Demo />);

Arguments​

ArgumentsTypeDescriptionDefault value
callbackfunctionFunction to be executed in timeoutundefind
delayNumberNumber in milliseconds after which callback is to be run0
whenbooleanThe condition which when true, sets the timeouttrue

Returned​

No return value.


Codesandbox Examples​

Basic Usage​

Join Bhargav's discord server​

You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.