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

@oscarpalmer/timer

v0.24.0

Published

A better solution for timeout- and interval-based timers.

Downloads

261

Readme

Timer

npm Tests

A better solution for timeout- and interval-based timers.

Installation

Timer is available on npm as @oscarpalmer/timer to be bundled with your awesome projects.

Getting started

This is fairly lightweight package, so hopefully you'll be up and running in seconds :blush:

Examples

The timers can be called with nice helper methods, which also auto-starts the timers:

import {repeat, wait} from '@oscarpalmer/timer';

const waited = wait(waitedCallback);
const repeated = repeat(repeatedCallback, 10);

Or they can be created using the new-keyword, but without being auto-started:

import {Timer} from '@oscarpalmer/timer';

const waited = new Timer(waitedCallback);
const repeated = new Timer(repeatedCallback, 10);

Parameters

When creating a Timer, either with the new new-keyword or using the functions, you can configure the timer with a few parameters:

|Parameter|Description| |--------:|:----------| |callback|Callback function to be invoked for each run that are required for all timers.For more information on callbacks, please read the callbacks section.| |count|How many times the timer should run.If no value is provided, it will default to 1 when using the new-keyword and the wait-method, but throws an error for the repeat-method.| |time|How many milliseconds between each invokations of the provided callback.Defaults to 0, which is not really 0 milliseconds, but close enough :wink:| |after|A callback to run after the timer finishes, both when cancelled and completed.If count is greater than 1 and after is not undefined, a function is expected.|

Methods and properties

An instance of Timer also has a few helpful methods and properties:

|Name|Type|Description| |---:|----|:----------| |active|Property|A boolean value to check if the timer is running| |finished|Property|A boolean value to check if the timer was able to finish| |start()|Method|Starts the timer.Necessary when creating a timer using the class syntax (e.g. new Waited...), but helpful when the timer needs to be started at other times, as well.| |stop()|Method|Stops the timer| |restart()|Method|Restarts the timer|

Callbacks

Callbacks for both waited and repeated timers receive one parameter:

function callback(index) {
	// 'index' is the current step
	// starts at 0, goes up to a maximum of count - 1
	// for this example: 0 → 9
};

When you create a repeated timer, you can also provide a callback to run when the timer stops, as below:

function after(finished: boolean) {
	// Let's do something fun!
}

repeat(() => {}, 10, after);

The finished-parameter for the after-function can be used to determine if the timer was stopped manually, or if it was able to finish its work.

License

MIT licensed, natch :wink: