npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@basementuniverse/stopwatch

v1.0.1

Published

A somewhat accurate timer for use in browser games.

Readme

Game Component: Stopwatch

A somewhat accurate timer for use in browser games.

Installation

npm install @basementuniverse/stopwatch

How to use

import Stopwatch from '@basementuniverse/stopwatch';

const stopwatch = new Stopwatch({
  unit: 's',
  rate: 1,
  direction: 'forward',
  max: Infinity,
});

// Update the stopwatch every frame
function gameLoop() {
  stopwatch.update();
  requestAnimationFrame(gameLoop);
}

// Start and stop the stopwatch
stopwatch.start();
stopwatch.stop(); // optionally pass StopwatchStopOptions to control reset behavior
stopwatch.running = true; // Start the stopwatch
stopwatch.running = false; // Stop the stopwatch

// Pause and resume the stopwatch
stopwatch.pause();
stopwatch.resume();
stopwatch.paused = true; // Pause the stopwatch
stopwatch.paused = false; // Resume the stopwatch

// Reset the stopwatch
stopwatch.reset();

// Manually adjust the time
stopwatch.adjustTime(5); // +5 seconds
stopwatch.adjustTime(-2); // -2 seconds

// Get values from the stopwatch
stopwatch.running; // boolean
stopwatch.paused; // boolean
stopwatch.time; // number
stopwatch.elapsed; // number
stopwatch.remaining; // number
stopwatch.progress; // number, [0, 1]
stopwatch.pausedDuration; // number

Options

type StopwatchOptions = {
  /**
   * The unit of time used by the stopwatch. This determines how the time values
   * are interpreted and displayed. The default unit is 'milliseconds'.
   */
  unit: 'ms' | 's';

  /**
   * The rate at which the stopwatch progresses. A rate of 1 means real-time,
   * 2 means twice as fast, and 0.5 means half as fast.
   */
  rate: number;

  /**
   * The direction in which the stopwatch counts.
   * 'forward' means it counts up, 'backward' means it counts down.
   */
  direction: 'forward' | 'backward';

  /**
   * The maximum value the stopwatch can reach. Must be finite when counting
   * backward.
   */
  max: number;

  /**
   * An array of milestone values. When the stopwatch reaches a milestone, it
   * can trigger a callback or perform an action.
   */
  milestones?: number[];

  /**
   * Callback function that is called when the stopwatch is started.
   */
  onStart: () => void;

  /**
   * Callback function that is called when the stopwatch is stopped.
   */
  onStop: () => void;

  /**
   * Callback function that is called when the stopwatch is paused.
   */
  onPause: () => void;

  /**
   * Callback function that is called when the stopwatch is resumed.
   */
  onResume: () => void;

  /**
   * Callback function that is called when the stopwatch finishes.
   *
   * When counting forward, the stopwatch finishes when it reaches the maximum
   * value. When counting backward, the stopwatch finishes when it reaches zero.
   *
   * If counting forward and max is Infinity, the stopwatch will never finish.
   */
  onFinish: () => void;

  /**
   * Callback function that is called when the stopwatch reaches a milestone
   * value.
   *
   * The milestone parameter is the index ofconst { Stopwatch } = the milestone in the milestones
   * array that was reached.
   */
  onMilestone?: (milestone: number) => void;
};

When stopping the stopwatch, you can specify options to control whether to reset the time and paused duration:

type StopwatchStopOptions = {
  /**
   * Whether to reset the time to 0 when stopping the stopwatch.
   *
   * Default is true.
   */
  resetTime: boolean;

  /**
   * Whether to reset the paused duration to 0 when stopping the stopwatch.
   *
   * Default is true.
   */
  resetPausedDuration: boolean;
};