useCountdown

A hook to manage countdown timers.

Overview

useCountdown manages a numeric countdown. It provides start, stop, and reset controls, and can be configured to count down or up.

Demo

Timer

Launch Sequencer

A countdown hook suitable for timers, games, or launches.

10

API Reference

function useCountdown(options: CountdownOptions): [number, CountdownControllers]

Options

NameTypeDescription
countStartnumberStarting value.
intervalMsnumberInterval speed in ms (default: 1000).
countStopnumberValue to stop at (default: 0).
isIncrementbooleanIf true, counts up instead of down.

Usage Example

import { useCountdown } from 'react-hooks-ts'
 
function Timer() {
  const [count, { startCountdown, stopCountdown, resetCountdown }] = useCountdown({
    countStart: 60,
    intervalMs: 1000
  })
 
  return (
    <div>
      <p>{count}</p>
      <button onClick={startCountdown}>Start</button>
      <button onClick={stopCountdown}>Stop</button>
      <button onClick={resetCountdown}>Reset</button>
    </div>
  )
}