useTimeout
Delay running callback.
API
function useTimeout(
callback: () => void,
timeout?: number
): [start: () => void, cancel: () => void];
Params
callback
: A function to be executed aftertimeout
time.timeout
: Optional delay time, in milliseconds.
Results
start
: Set a timer. Automatically clear the previous timer.cancel
: Cancel timer.
Example
Count: 0
import React, { useState } from "react";
import { useTimeout } from "@lilib/hooks";
function Example() {
const [count, setCount] = useState(0);
const [increase, cancel] = useTimeout(() => {
setCount((count) => ++count);
}, 1000);
return (
<>
<button onClick={increase}>+1 after one second</button>{" "}
<button onClick={cancel}>Cancel</button> Count: {count}
</>
);
}
export default Example;