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

ntimer

v0.5.3

Published

Event firing, repeatable, cancellable, auto-start capable timer for nodejs

Downloads

8

Readme

ntimer

An event firing, repeatable, cancellable timer for nodejs.

Build Status npm version

Installation

Install with npm

npm install ntimer --save

Examples

Just a Timer

ntimer = require 'ntimer'

# Creates an manual-start timer which fires after 2000ms
ntimer('foo', '2s')
.on "done", -> # do something
.start()

Auto Start

# Creates an auto-start timer which fires after 200ms
# Must attach a 'timer' event listener to enable auto start
ntimer.auto('foo', 200)
.on "timer", -> # do something

Cancel Timer

# starts a second timer when the first starts which cancels 
# the first timer before the first timer ends.
ntimer.auto('foo', '5s')
.on "start", -> setTimeout ( => @cancel()), 500
.on "done", -> # do something
.on "cancel", -> # why, oh why?

Repeat

# Create an infinitely repeating timer whch fires every 500ms.
ntimer.repeat('foo', '500ms')
.on "timer", (name, count) -> # do something
.on "done", -> # never called
.on "cancel", -> # called if timer is cancelled
.start()

Limited Repeat

# Create a timer which fires every 500ms for five times.
ntimer.autoRepeat('foo', '500ms', 5)
.on "timer", (name, count) -> # do something
.on "done", -> # fired after the fifth 'timer' event

API

Create Timer

ntimer(name, timeout)

ntimer.auto(name, timeout)

Creates a single shot, restartable timer. ntimer.auto creates an auto starting timer.

  • name {String} is returned as the first argument of events.
  • timeout can be a {Number} in milliseconds or a {String} e.g. '5s' or '2ms' . See format.
ntimer('foo', 500)  # Single shot, manual start, 500ms timer.

Note: An auto-starting timer will start only when it has a event listener for the trigger event.

t = ntimer.auto('foo', 500)  # Create an auto start timer, 500ms

# The auto-start timer will start only after this listener is attached
t.on 'trigger', -> # do something

ntimer.repeat(name, timeout[, maxRepeat])

ntimer.autoRepeat(name, timeout[, maxRepeat])

Creates a repeating timer. ntimer.auto creates an auto starting version.

  • name {String} is returned as the first argument of events.
  • timeout can be a {Number} in milliseconds or a {String}. See format.
  • maxRepeat an optional {Number} specifies the maximum repeat count.
ntimer.repeat('foo', 500)  # Single shot, 500ms timer.
ntimer('foo', '2s')  # Single shot, 2 second timer.

Properties

timer.count

The {Number} of times the repeating timer has already fired.

timer.running

{Boolean} indicating whether the timer is currently running.

Methods

timer.start()

Start a timer. No-op if already started. Returns the timer object for chaining.

timer.cancel()

Cancels a running timer. No-op if not started or already stopped/cancelled. Returns the timer object for chaining.

Events

on("start", cb(name))

Fired when the timer is started (or restarted) with the name of the timer.

on("cancel", cb(name))

Fired when the timer is cancelled with the name of the timer.

on("done", cb(name))

Fired when the timer is done (not cancelled) with the name of the timer.

on("timer", cb(name, count))

Fired for each timeout interval, once for single shot timers, or repeatedly for repeating timers, with the name of the timer and the the count of the times the timer has triggered so far.

ntimer.Timer Class

ntimer exports the Timer class.

String Formats for Time

Uses the millisecond module. The following formats are acceptable:

  • x milliseconds
  • x millisecond
  • x msecs
  • x msec
  • x ms
  • x seconds
  • x second
  • x secs
  • x sec
  • x s
  • x minutes
  • x minute
  • x mins
  • x min
  • x m
  • x hours
  • x hour
  • x hrs
  • x hr
  • x h
  • x days
  • x day
  • x d
  • x weeks
  • x week
  • x wks
  • x wk
  • x w
  • x years
  • x year
  • x yrs
  • x yr
  • x y

The space after the number is optional so you can also write 1ms instead of 1 ms.