Skip to content

[RFC] useTimer #2625

Description

@jacksyk

针对时间管理的一些方法,如倒计时和计时器。将关于时间的操作统一收敛到一个hooks里面。例如,可以毫无心智负担地去做倒计时功能,从而在此基础上去完善其业务逻辑,业务逻辑不用包含过多的倒计时功能

API

const returnValue= useTimer<T extends number| Date >(
  time: number | Date,
  options: Options,
  deps: DependencyList = [],
):ReturnValue<T>;

Demo

const { seconds, minutes, hours, days, start, pause, reset, isPaused } = useTimer<number>(
    1000 * 3,
    {
      onComplete: () => {
        Message.info('计时完成');
      },
      auto: false
    },
  );

  return (
    <>
      {isPaused && <span>暂停中</span>}
      <div>
        {days ? <span>{days}天 </span> : null}
        <span style={{ fontSize: 20 }}>
          {hours}:{minutes}:{seconds}
        </span>
      </div>
      <Button style={{marginRight: 10}} type="primary" onClick={() => start()}>
        开始/恢复
      </Button>
      <Button style={{marginRight: 10}} type="primary" onClick={() => pause()}>
        暂停
      </Button>
      <Button style={{marginRight: 10}} type="primary" onClick={() => reset()}>
        复位
      </Button>
    </>
  );

pr #2626

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions