Skip to content
usefy

useTimer

Timing & async

A powerful, accurate countdown timer hook for React applications

Install

$ npm install @usefy/use-timer

Quick start

useTimer.tsx
import { useTimer, ms } from "@usefy/use-timer";

function Timer() {
  const timer = useTimer(ms.minutes(5), { format: "MM:SS" });

  return (
    <div>
      <p>{timer.time}</p>
      <button onClick={timer.toggle}>
        {timer.isRunning ? "Pause" : "Start"}
      </button>
      <button onClick={timer.reset}>Reset</button>
    </div>
  );
}

API reference

useTimer(initialTimeMs, options?)

A hook that manages countdown timer state with comprehensive controls.

Parameters

ParameterTypeDescription
initialTimeMsnumberInitial countdown time in milliseconds
optionsUseTimerOptionsOptional configuration object

Options

OptionTypeDefaultDescription
intervalnumber1Update interval in ms. Coarse formats re-render at most once per second, so prefer a larger value (e.g. 250) for them and reserve 1 for millisecond-precision formats.
formatTimeFormat | TimeFormatter"MM:SS"Time display format or custom formatter function
autoStartbooleanfalseStart timer automatically on mount
loopbooleanfalseRestart timer automatically when it completes
useRAFbooleanfalseUse requestAnimationFrame instead of interval
onComplete() => void-Callback when timer reaches 0
onTick(remaining: number) => void-Callback on each tick with remaining time
onStart() => void-Callback when timer starts
onPause() => void-Callback when timer is paused
onReset() => void-Callback when timer is reset
onStop() => void-Callback when timer is stopped

Time Formats

FormatExampleDescription
"MM:SS"05:30Minutes and seconds
"HH:MM:SS"01:05:30Hours, minutes, and seconds
"SS"330Total seconds
"mm:ss.SSS"05:30.123Minutes, seconds, and milliseconds
"HH:MM:SS.SSS"01:05:30.123Full format with milliseconds
Custom functionAny string(ms: number) => string

Returns UseTimerReturn

PropertyTypeDescription
timestringFormatted time string (e.g., "05:30")
progressnumberProgress percentage (0-100)
statusTimerStatusCurrent status: idle, running, paused, finished
isRunningbooleanWhether timer is running
isPausedbooleanWhether timer is paused
isFinishedbooleanWhether timer has finished
isIdlebooleanWhether timer is idle
start() => voidStart the timer
pause() => voidPause the timer
stop() => voidStop timer (keep current time)
reset() => voidReset to initial time
restart() => voidReset and immediately start
toggle() => voidToggle between start/pause
addTime(ms: number) => voidAdd time to the timer
subtractTime(ms: number) => voidSubtract time from the timer
setTime(ms: number) => voidSet timer to a specific value

ms Helper Object

A utility object for converting time units to milliseconds.

import { ms } from "@usefy/use-timer";

ms.seconds(30); // 30000
ms.minutes(5); // 300000
ms.hours(1); // 3600000

// Combine them
ms.hours(1) + ms.minutes(30); // 1h 30m = 5400000

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in timing & async