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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rx-timer

v1.1.0

Published

A timer utility using RxJS for countdown functionality.

Downloads

3

Readme

RxTimer

License Node.js CI

RxTimer is a utility for countdown timers, offering observable events for timer management in various applications.

Installation

To install the RxTimer utility, use npm:

npm install --save rx-timer

Usage

Import the RxTimer class into your project:

import { RxTimer, RxTimerEvent } from "rx-timer";

Create an instance of RxTimer and start:

const timer = new RxTimer(5000); // Timer duration: 5000 milliseconds (5 seconds)
timer.start();

API

Options

continue

When set to true, this property allows the timer to automatically initiate the subsequent countdown cycle upon pausing until the 'stop()' method is invoked.

Alternatively, it allows pausing through the 'pause()' method and resumes counting upon the next 'resume()' invocation.

const timer = new RxTimer(1000, { continue: true });

timer.start(); // timer will emit onTick at every second

beginTime

Specifies the time in milliseconds when the timer should start counting down. If the current time has already passed the specified beginTime, the timer will immediately start counting down.

const timestamp = new Date("2023-12-22T13:21:39+08:00").getTime();
const timer = new RxTimer(1000, { beginTime: timestamp });

timer.start(); // timer will start with 2023-12-22T13:21:39+08:00

Methods

start()

Initiates a timing cycle based on the provided duration and emits Tick events upon completion. Pause() can be used to pause the cycle, while stop() halts the ongoing timing cycle.

const timer = new RxTimer(1000);

timer.start(); // start to counting

pause()

Pauses the ongoing timer and resumes from the remaining time upon the next execution of 'resume()'.

const timer = new RxTimer(1000);

timer.start();

setTimeout(() => {
  timer.pause(); // pauses the timer after 500 milliseconds
}, 500);

resume()

Resumes timing from the paused state, continuing the countdown from the remaining time and triggers the 'Tick' event upon completion.

const timer = new RxTimer(1000);

timer.start();

setTimeout(() => {
  timer.pause(); // pauses the timer after 500 milliseconds

  timer.resume(); // resumes the timer immediately after pausing and continues the remaining 500ms
}, 500);

~~reset()~~

Resets the countdown and clears any active subscription.

  • Deprecated: Unable to differentiate distinctions within 'stop()'. Use 'stop()'. Expected removal in version 2.0.0.
const timer = new RxTimer(1000);

timer.start(); // start counting

timer.reset(); // reset counting

stop()

Stop the timer and reset the remaining time. Resuming the timer using 'resume()' is not possible after stopping; it needs to be initiated again with 'start()' to begin the next timing cycle.

const timer = new RxTimer(1000);
timer.start(); // start counting

timer.stop(); // stop counting

isCounting()

Checks if the timer is currently in a counting state.

const timer = new RxTimer(1000);

timer.isCounting(); // false

timer.start();

timer.isCounting(); // true

isStopped()

Checks if the timer is currently in a stopped state.

const timer = new RxTimer(1000);

timer.isStopped(); // true

timer.start();

timer.isStopped(); // false

isPaused()

Checks if the timer is currently in a paused state.

const timer = new RxTimer(1000);

timer.start();

timer.isPaused(); // false

timer.pause();

timer.isPaused(); // true

getRemainingMilliseconds()

Retrieves the remaining time on the timer in milliseconds.

const timer = new RxTimer(1000);

timer.getRemainingMilliseconds(); // 0

timer.start();

setTimeout(() => {
  timer.getRemainingMilliseconds(); // 487
}, 500);
const timer = new RxTimer(1000, { beginTime: new Date().getTime() + 1000 });

timer.start();

setTimeout(() => {
  timer.getRemainingMilliseconds(); // 0
}, 500);

setTimeout(() => {
  timer.getRemainingMilliseconds(); // 498
}, 1500);

Note: When the specified beginTime has not yet been reached, getRemainingMilliseconds() will return 0, and the timer state will be considered as 'stopped'.

Observables

onStart()

Triggers an event to start a brand new timer when 'start()' is invoked. Note: If the timer is paused, this event won't be received, and the timer won't restart. Use 'resume()' to resume a paused timer.

timer.onStart().subscribe(() => {
  // Handle start event
});

onPause()

Triggers an event to pause the timer upon 'pause()' invocation. Note: Non-running timers won't receive this event upon 'pause()' invocation. For timers set with 'beginTime', pausing before the timer starts also won't trigger this event.

timer.onPause().subscribe(() => {
  // Handle pause event
});

onResume()

Upon 'resume()' invocation, triggers an event to resume the timer.

timer.onResume().subscribe(() => {
  // Handle resume event
});

onStop()

Triggers an event upon invoking 'stop()' while the timer is running.

timer.onStop().subscribe(() => {
  // Handle stop event
});

onTick()

Triggers an event upon completion of a timing cycle.

timer.onTick().subscribe(() => {
  // Handle tick event
});

onEvent()

All events related to the timer's state transition can be listened to from here.

timer.onEvent().subscribe((event: RxTimerEvent) => {
  // Handle any timer event
});

Example

const timer = new RxTimer(10000); // Timer duration: 10 seconds

timer.onTick().subscribe(() => {
  console.log("Tick!");
});

timer.start();

License

This project is licensed under the MIT License - see the LICENSE file for details.