UNPKG

countdown-pro

Version:
176 lines (175 loc) 4.87 kB
import { format, parseTimeData, parseFormat, padZero } from './util'; export type Options = { /** * 倒计时,单位毫秒 */ time: number; /** * 时间间隔,单位毫秒 * @default 1000 */ interval?: number; /** * 自动校准倒计时时间间隔,单位毫秒。 * * 如果有值且大于 `0`,再开始倒计时时会执行自动校准定时任务。暂定或结束倒计时会停止定时任务。 */ adjustInterval?: number; /** * 倒计时时间变动时触发 * @param {number} currentTime 当前倒计时时间,单位毫秒 * @returns */ onChange?: (currentTime: number) => void; /** * 倒计时结束时触发 * @returns */ onEnd?: () => void; }; /** * 倒计时 * * @param {Object} options 配置项 * @param {number} options.time 倒计时,单位毫秒 * @param {number} [options.interval=1000] 时间间隔,单位毫秒。默认 `1000` * @param {Function} [options.onChange] 倒计时时间变动时触发 * @param {Function} [options.onEnd] 倒计时结束时触发 * @param {number} [options.adjustInterval] 自动校准倒计时时间间隔,单位毫秒。如果有值且大于 `0`,再开始倒计时时会执行自动校准定时任务。暂定或结束倒计时会停止定时任务。 * @returns * @example * const countdown = new Countdown({ * time: 60 * 1000 * interval: 1000, * onChange(){}, * onEnd(){}, * adjustInterval: 10 * 1000 * }); * * // 实例方法 * // 开始倒计时 * countdown.start(); * * // 暂停倒计时 * countdown.pause(); * * // 重置倒计时。先暂停再将倒计时时间重置 * countdown.reset(); * * // 重置再开始倒计时 * countdown.restart(); * * // 更新配置。如果更新 `time` 需要手动调用 `reset` 或 `restart` 方法才生效 * countdown.updateOptions(options); * * // 校准倒计时。在倒计时运行时才生效,如果需要校准会先暂停再开始。 * countdown.adjustTime(); * * // 静态方法 * // 格式化时间,返回格式化后的时间字符串 * CountDown.format(2 * 60 * 60 * 1000); // "02:00:00" * CountDown.format(2 * 60 * 60 * 1000, 'mm:ss'); // "120:00" * * // 前置补零,返回补零后的值 * CountDown.padZero(2); // "02" * * // 解析时间戳,返回的时间对象格式 * CountDown.parseTimeData(2 * 60 * 60 * 1000); * // {days: 0, hours: 2, minutes: 0, seconds: 0, milliseconds: 0} * */ declare class CountDown { private o; protected options: Required<Options>; /** * 定时器 */ private timer; /** * 标识正在倒计时 */ private counting; /** * 标识倒计时完成 */ private completed; /** * 记录当前倒计时长 */ private currentTime; /** * 记录开始时间戳,用于校准 */ private adjustStartTime; /** * 记录开始的倒计时长,用于校准 */ private adjustCurrentTime; /** * 校准倒计时时间定时器 */ private adjustTimer; constructor(options: Options); private _handleEnd; /** * 更新配置。 * * 如果更新 `time` 需要手动调用 `reset` 或 `restart` 方法才生效。 * * @param {Object} options 配置项 * @param {number} [options.time] 倒计时,单位毫秒 * @param {number} [options.interval] 时间间隔,单位毫秒 * @param {Function} [options.onChange] 倒计时时间变动时触发 * @param {Function} [options.onEnd] 倒计时结束时触发 * @returns */ updateOptions(options: Partial<Options>): void; /** * 开始倒计时 * @returns */ start(): void; /** * 校准倒计时时间。建议使用 `adjustInterval` 配置。 * * 仅在倒计时运行时才生效。如果需要校准,会先暂停,调用 `onChange` ,再开始倒计时。 * * @example * const interval = 1000; * let timer = null; * const countdown = new CountDown({ * time: 60 * 1000, * interval, * onEnd(){ * clearInterval(timer); * } * }); * * // 每倒计时10次,校准一次倒计时时间 * const timer = setInterval(()=>{ * countdown.adjustTime(); * }, interval * 10); * */ adjustTime(): void; /** * 暂停倒计时 */ pause(): void; /** * 重置倒计时。 * * 先暂停再将倒计时时间重置。 */ reset(): void; /** * 重置再开始倒计时 */ restart(): void; private tick; static format: typeof format; static parseTimeData: typeof parseTimeData; static parseFormat: typeof parseFormat; static padZero: typeof padZero; } export default CountDown;