React Use Precision Timer
Edit page
HomeOverviewInstallationQuick StartTypeScriptContributing⭐ Found It Helpful? Star It!Demo
Hooks
Code of ConductMIT License

React Use Precision Timer

A versatile precision timer hook for React. Doubles as a stopwatch.

👁️ Live Demo

Overview

A React timer hook that calls the provided callback at regular intervals. Can be used as a stopwatch, too.

It's accurate, precise, and includes a rich set of options, functionality, and accessors.

Features include:

  • ⏰ Timeout and timestamp based
    • Based on setTimeout() and Unix times, not ticks.
  • 🎯 Accurate and precise
    • Perfect mean accuracy with no wandering, with sub 10ms callback precision.
  • 💪 Doesn't choke under pressure
    • Resilient to expensive callbacks and low timer delays.
  • 🧰 Versatile
    • Can be used as a timer, one-time delay, or stopwatch. Additional options available.
  • ⏯️ Pause and resume
    • Supports pausing, and tracks cumulative elapsed pause time between resumes.
  • 🌞 Accessors for everything
    • Includes getters for everything under the sun! Know all the things.

Installation

npm i react-use-precision-timer

Quick Start

Repeating Timer

import { useTimer } from 'react-use-precision-timer';

In your function component:

// The callback will be called every 1000 milliseconds.
const timer = useTimer({ delay: 1000, callback: () => console.log("Boom") });

In a handler or effect:

timer.start();

The following functions can be used to control the Timer:

  • timer.start() - Start the timer. If already started, will restart the timer.
  • timer.stop() - Stop the timer.
  • timer.pause() - Pause the timer.
  • timer.resume() - Resume the timer.

Refer to Timer for all available functions, including getters for elapsed times.

One-Time Delay

If you'd like to run a callback after a one-time delay, use the helper hook useDelay:

import { useDelay } from 'react-use-precision-timer';
// Call once after 1000ms.
useDelay(1000, () => console.log("Boom"));

The provided callback will be called only once after the provided delay.

Stopwatch

The timer also functions as a stopwatch when no delay is provided. You can use the helper hook useStopwatch:

import { useStopwatch } from 'react-use-precision-timer';
const stopwatch = useStopwatch();

Use start(), stop(), pause(), and resume() to control the stopwatch.

Stopwatch is a Timer object. Refer to Timer's getters to retrieve elapsed running time, paused time, and so forth.

Calling start while a stopwatch is already running will restart it.

Other Usage

See useTimer for all other hook options and timer functions.

TypeScript

Type definitions have been included for TypeScript support.

Contributing

Open source software is awesome and so are you. 😎

Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.

For major changes, open an issue first to discuss what you'd like to change.

See Kindling for npm script documentation.

⭐ Found It Helpful? Star It!

If you found this project helpful, let the community know by giving it a star: 👉⭐