Docs
useStopwatch

Create a stopwatch with useStopwatch, providing options to pause, play, reset, and toggle the stopwatch.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-stopwatch

Usage

"use client"
 
import { useStopwatch } from "@/hooks/use-stopwatch"
 
export function Component() {
  const {
    current,
    isPaused,
    isOver,
    currentDays,
    currentHours,
    currentMinutes,
    currentSeconds,
    elapsedSeconds,
    pause,
    play,
    reset,
    togglePause,
  } = useStopwatch()
 
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: "16px" }}>
      <p>Counter value: {current}</p>
      <p>Is the counter paused? {isPaused ? "Yes" : "No"}</p>
      <p>Has the counter over? {isOver ? "Yes" : "No"}</p>
      <p>Current days: {currentDays}</p>
      <p>Current hours: {currentHours}</p>
      <p>Current minutes: {currentMinutes}</p>
      <p>Current seconds: {currentSeconds}</p>
      <p>Elapsed seconds: {elapsedSeconds}</p>
      <button type="button" onClick={pause}>
        Pause
      </button>
      <button type="button" onClick={play}>
        Play
      </button>
      <button type="button" onClick={reset}>
        Reset
      </button>
      <button type="button" onClick={togglePause}>
        Toggle Pause
      </button>
    </div>
  )
}

API Reference

Return Values

NameTypeDescription
currentnumberThe current value of the stopwatch.
isPausedbooleanWhether the stopwatch is paused.
isOverbooleanWhether the stopwatch is over.
currentDaysnumberThe current days of the stopwatch.
currentHoursnumberThe current hours of the stopwatch.
currentMinutesnumberThe current minutes of the stopwatch.
currentSecondsnumberThe current seconds of the stopwatch.
elapsedSecondsnumberThe elapsed seconds of the stopwatch.
pause() => voidPause the stopwatch.
play() => voidPlay the stopwatch.
reset() => voidReset the stopwatch.
togglePause() => voidToggle the pause state of the stopwatch.