A simple JS timer library, allowing you to subscribe to timer updates
  • TypeScript 95.9%
  • Shell 4.1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Noah Rosenzweig 240e14fe63 Version 1.1.3
2022-01-12 16:47:40 +01:00
.github/workflows Setup run-examples job in run-tests workflow to test that all examples run successfully 2021-06-24 23:29:24 +02:00
bin Run all examples in bin/run-example 2021-06-14 09:40:25 +02:00
docs Fix command to update version list in docs/index.html 2021-06-04 04:51:14 +02:00
examples Add example unsubscribe.js 2021-06-24 23:37:56 +02:00
src Update internal update function to only update time when timer is playing 2022-01-12 16:46:25 +01:00
tests Add package mockdate, add timing tests to test specific timer bug 2022-01-12 16:46:54 +01:00
.gitignore Ignore .log files in gitignore 2021-06-04 06:03:16 +02:00
.prettierrc Initial commit 2021-06-02 07:13:31 +02:00
jest.config.ts Setup jest with typescript 2021-06-02 11:49:38 +02:00
LICENSE Add README and LICENSE 2021-06-02 10:22:51 +02:00
package.json Version 1.1.3 2022-01-12 16:47:40 +01:00
README.md Update README with examples section 2021-06-24 23:34:01 +02:00
tsconfig.json Setup jest with typescript 2021-06-02 11:49:38 +02:00
tsconfig.test.json Split-up unit tests in src/timer.test.ts into separate files in tests/ 2021-06-08 20:46:36 +02:00
yarn.lock Add package mockdate, add timing tests to test specific timer bug 2022-01-12 16:46:54 +01:00

timesub

A simple JS timer library, with easy subscription to the updating time.

Install

npm install --save timesub

or

yarn add timesub

Usage

import { createTimer } from "timesub";

// Optional timer options.
// You can omit any of these properties.
// These are the default values:
const options = {
    duration: "infinite" // "infinite" or number, to set the duration.
    updateInterval: 100, // Delay in milliseconds between every update.
};

// Create the timer.
const timer = createTimer(options);

// Start the timer.
timer.play();

// SUBSCRIBE API
// You can subscribe to time updates, by passing a
// callback function to the `subscribe` function.
// Returns an "unsubscribe" function, which you can call to, uuuh, unsubscribe.
const unsubscribe = timer.subscribe(
    ({ time, ...timer }, event) => {
        console.log(`Current time: ${time}`);

        if (event.type === "finish") {
            console.log("Timer finished!")
        }
    },
);

// Unsubscribe by calling the returned function.
unsubscribe();

// EVENT LISTENER API
// Similar to the `subscribe` function, you can listen to specific events.
// The callback function gets the same arguments as in the `subscribe` callback.
const unsubscribeListener = timer.on("finish", ({ time, ...timer }, event) =>
    console.log(`Timer finished at ${time}!`),
);

unsubscribeListener();

// API
timer.play();
timer.pause();
timer.togglePlay();
timer.reset(); // Resets back to 0 and stops the timer.
const currentTime = timer.getTime();
timer.setTime(1000); // Set a new time in milliseconds.

// For more available API functions see the docs...

Documentation

API documentation is available here.
Generated by typedoc and hosted on GitHub pages.

Examples

See the examples/ directory for usage examples.
You can run the examples with:

yarn run example <exampleFile.js>

Licensing

Distributed under the terms of the MIT license.