useTimeout
Update value after a given time
Usage
Live Editor
function Demo() { const [isPending, start, cancel] = useTimeout(5000); return ( <div> <div>Pending: {JSON.stringify(isPending)}</div> <button onClick={() => { start(); }} > Start Again </button> <button onClick={() => { cancel(); }} > Cancel </button> </div> ); };
Result
Loading...
API
useTimeout
Returns
Stoppable
: A tuple with the following elements:
- Whether to wait for the timer to execute.
- Set timer.
- Cancel timer.
Arguments
Argument | Description | Type | DefaultValue |
---|---|---|---|
ms | wait time | number | undefined | - |
options | - | UseTimeoutOptions | undefined | - |
UseTimeoutOptions
Property | Description | Type | DefaultValue |
---|---|---|---|
immediate | Start the timer immediate after calling this function | boolean | false |