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

@joepie91/timer

v1.0.1

Published

A resettable timer abstraction, with cancellation events

Downloads

11

Readme

@joepie91/timer

A resettable timer abstraction, with cancellation events. Typical usecases might include handling screen display timeouts, debouncing actions, and so on.

Calling the function exported by this module will get you a timer object, which can be started/stopped as needed. Starting an already-running timer first stops it (triggering the cancellation handler) and then starts it again.

The timeout and handlers can be defined both in the initial function call that creates the object, as well as in the arguments to the start(...) method.

The full API documentation can be found below the usage examples.

Examples

A simple example:

"use strict";

const timer = require("@joepie91/timer");

let someTimer = timer({
	timeout: 500,
	onFire: () => {
		console.log("Fired 1");
	},
	onCancel: () => {
		console.log("Cancelled 1");
	}
});

someTimer.start();
/*
After 500ms:
	Fired 1
*/

A more complex example, demonstrating resettability and cancellation handlers:

"use strict";

const timer = require("@joepie91/timer");

let someTimer = timer({
	timeout: 500,
	onFire: () => {
		console.log("Fired 1");

		someTimer.start({
			onFire: () => {
				console.log("Fired 2");
			}
		});

		setTimeout(() => {
			someTimer.start({
				timeout: 600,
				onFire: () => {
					console.log("Fired 3");
				}
			});
		}, 200);
	},
	onCancel: () => {
		console.log("Cancelled!");
	}
});

someTimer.start();
/*
After 500ms:
	Fired 1
200ms later:
	Cancelled!
600ms later:
	Fired 3
*/

API

timer(options)

Returns a new timerObject. The returned timer does not start running until you call its start method!

Arguments:

  • options:
    • timeout: The timeout, in milliseconds, after which the timer should 'fire' by default.
    • onFire: A callback that will be called when the timer fires (ie. hits the timeout).
    • onCancel: Optional. A callback that will be called when the timer is cancelled (ie. stopped prematurely).

timerObject.start([options])

Starts the timer (anew), first stopping it if it's already running.

Arguments:

  • options: Optional. These settings will override the options that were used to create the timerObject, but only for this run.
    • timeout: Optional. The timeout, in milliseconds, after which the timer should 'fire' on this run.
    • onFire: Optional. A callback that will be called when the timer fires on this run.
    • onCancel: Optional. A callback that will be called when this run of the timer is cancelled.

timerObject.stop()

Attempts to stop the timer, triggering the cancellation handler if the timer was still running. If the timer was already stopped, this does nothing.

timerObject.isRunning()

Returns a boolean indicating whether the timer is currently running or not.

Changelog

1.0.1 (August 25, 2019)

  • Fixed documentation formatting.

1.0.0 (August 25, 2019)

Initial release.