Skip to main content
Version: v4.7.0

use-timeout

About​

setTimeout hook for react.


Installation​

npm install --save @rooks/use-timeout

Importing the hook​

import useTimeout from "@rooks/use-timeout"

Usage​

function TimeoutComponent() {
function doAlert() {
window.alert("timeout expired!");
}
const { start, clear } = useTimeout(doAlert, 2000);
return (
<>
<button onClick={start}> Start timeout </button>
<button onClick={clear}> Clear timeout </button>
</>
);
}

render(<TimeoutComponent/>)

Arguments​

ArgumentsTypeDescriptionDefault value
callbackfunctionFunction to be executed in timeoutundefind
delayNumberNumber in milliseconds after which callback is to be run0

Returned Object keys​

Returned object attributesTypeDescription
clearfunctionClear the timeout
startfunctionStart the timeout
isActivebooleanIs the timeout active

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.