1.1.1 • Published 3 months ago

react-dead-simple-stopwatch-hook v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

⏱️ React Dead Simple Stopwatch Hook

Features ✨

  • Simple stopwatch hook
  • Basic features like pause, unpause, start, stop
  • Small size and no dependencies
  • Uses Date.now for accurate readings

Install ⬇

npm i react-dead-simple-stopwatch-hook

Quickstart ⚡

import { useStopwatch, decorateTimeStamp } from 'react-dead-simple-stopwatch-hook';

function App() {
const {
  currentTime,
  isTimerActive,
  start,
  stop,
  pause,
  resume
} = useStopwatch();

  return (
    <>
        <button onClick={() => start()}>start</button>
        <button onClick={() => stop()}>stop</button>
        <button onClick={() => pause()}>pause</button>
        <button onClick={() => resume()}>resume</button>

        <span>{decorateTimeStamp(currentTime)}</span>
    </>
  )
}

useStopwatch API

params

valuetypedefault valuedescription
options.customStartTimenumberDate.now()Adds a custom start time if you want to start the timer late
onStart, onStop, onPause() => void | undefinedundefinedCalls custom function before start, stop, pause

return

valuetypedescription
currentTimenumberThe current type returned as a difference of epoch timestamp, i.e. the result of Date.now()
startTimenumberThe epoch timestamp from which we started the watch
isTimerActiveboolean | nullTimer running state
timerRefMutableRefObject<number | null>Ref for the setInterval
timerState"paused" | "stopped" | "resumed"Simplified state for the timer
start()() => voidStarts the timer from startTime
stop()() => voidStops the timer completely. Starting the timer from here starts from 00:00:00
pause()() => voidPauses the timer
resume()() => voidResumes the timer. Works only when timer is paused and not stopped.

decorateTimeStamp

params

valuetypedefault valuedescription
msnumber0Epoch of the time to change

return

valuetypedescription
formattedTimestringtimestamp in the format mm:ss:ms
1.1.1

3 months ago

1.1.0

3 months ago

1.0.0

3 months ago