useCountDown
React State Hooks that return the minutes gracefull
Usage
实时编辑器
function Demo() { const now = new Date(); const tomorrow = new Date(); tomorrow.setDate(now.getDate() + 1); tomorrow.setHours(0, 0, 0, 0); const diffInSec = Math.floor((tomorrow.getTime() - now.getTime()) / 1000); // note: If your app is running in server side, must pass the same time as the client // this demo is not running in server side const [hour, minute, second] = useCountDown(diffInSec); return ( <div suppressHydrationWarning={true}>{`${hour}:${minute}:${second}`}</div> ); };
结果
Loading...
API
useCountdown
Returns
readonly [string, string, string]
: 包含以下元素的元组:
- 小时。
- 分钟。
- 秒数。
Arguments
参数名 | 描述 | 类型 | 默认值 |
---|---|---|---|
time | 时间差 | number (必填) | - |
format | 时间格式化函数 | ((num: number) => [string, string, string]) | undefined | HH MM SS |
callback | 倒计时结束的回调函数 | (() => void) | undefined | - |