rup

react-use-precision-timer

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

Showing:

Popularity

Downloads/wk

99

GitHub Stars

10

Maintenance

Last Commit

7mos ago

Contributors

2

Package

Dependencies

0

Size (min+gzip)

1.6KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

React Use Precision Timer

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

npm Version Tests Status Deploy Status

Documentation

Read the official documentation.

Demo

👁️ 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.

Logo Attribution

Logo graphics by Twemoji, licensed under CC-BY 4.0. Favicon by favicon.io.

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: 👉⭐

MIT License

Copyright © 2020 Justin Mahar https://github.com/justinmahar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100