useCountDown
返回分钟倒计时的 React 状态钩子。
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); // 如果您的应用在服务器端运行,必须传递与客户端相同的时间 // 这个 demo 没有在服务器端运行 const [hour, minute, second] = useCountDown(diffInSec); return ( <div suppressHydrationWarning={true}>{`${hour}:${minute}:${second}`}</div> ); };
结果
06:16:49
API
useCountdown
Returns
readonly [string, string, string]: 包含以下元素的元组:
- 小时。
- 分钟。
- 秒数。
Arguments
| 参数名 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| time | 时间差 | number (必填) | - |
| format | 时间格式化函数 | ((num: number) => [string, string, string]) | undefined | HH MM SS |
| callback | 倒计时结束的回调函数 | (() => void) | undefined | - |