useCountdown
Aboutβ
Count down to a target timestamp and call callbacks every second (or provided peried)
Installationβ
npm install --save rooks
Importing the hookβ
import { useCountdown } from "rooks";
Usageβ
const endTime = new Date(Date.now() + 10000);
function Demo() {
const count = useCountdown(endTime, {
interval: 1000,
onDown: (time) => console.log("onDown", time),
onEnd: (time) => console.log("onEnd", time),
});
return count;
}
render(<Demo />);
Argumentsβ
Argument | Type | Description | Default value |
---|---|---|---|
endTime | Date | the time when the countdown should end | undefined |
options.interval | number | milliseconds that it takes count down once | 1000 |
options.onDown | function | (time) => {}, callback that would be called every interval | undefined |
options.onEnd | function | (time) => {}, callback that would be called when the countdown ends | undefined |
Return Valueβ
Type | Description |
---|---|
number | rest amount of intervals it takes to count down to the endTime |
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.