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

@souct/ticker

v1.5.4

Published

An interval Ticker class (no GUI)

Downloads

5

Readme

TM-Ticker

License: MIT Build Status

An interval ticker class (no GUI).

Installation

$ npm install tm-ticker
import Ticker from 'tm-ticker';
// or
const Ticker = require('tm-ticker');

TL;DR

Jump to API

Create:

// construct & config
const t = new Ticker(interval, callback, tickOnStart = true)

// or just construct (and config later)
const t = new Ticker()

Config:

t.tickOnStart = bool // deafult: true
t.setInterval(number)
t.setCallback(fn)
t.set(interval, callback)

Use:

now is optional

 t.start(now)
 t.getTimeLeft(now)
 t.stop(now)
 t.reset(now)
 t.destroy()

API


Constructor

new Ticker(interval, callback, tickOnStart);
  • interval [number, optional]
    Milliseconds between ticks. Must be greater than 50.

  • callback [function, optional]
    Tick handler function. Gets called on every tick.

  • tickOnStart [boolean, optional, default: true]
    By default, the first tick happens on start, synchronously, before any timeout. Set to false if you want the first tick to happen only after the first interval.

Configuration

A Ticker instance won't tick unless it has an interval and a tick callback.
You can do it on construction or later with the following methods which are very self explanatory:

const myTicker = new Ticker();

myTicker.setInterval(1000)
myTicker.setCallback(myFunc)
myTicker.set(1000, myFunc)

// default is true.
myTicker.tickOnStart = false

You can also set the interval & callback directly as props but this way bypasses type validation for both and min number validation for interval (should be greater than 50ms).

myTicker.interval = 1000
myTicker.callback = myFunc

Methods

All methods can get called with a timestamp argument. Pass in a current timestamp when you need to sync time with other modules.

  • timestamp (ms, number, optional) - The timestamp to be considered as the method's execution time.

.start()

Start ticking.

If tickOnStart is set to true (default behavior), your callback will get called on start (as opposed to only after the first interval)

When called after a .stop() it acts as a "resume" function. There will be no start-tick in this case. The next tick is calculated based on the timeLeft record.

// optional
const timestamp = Date.now()

myTicker.start(timestamp)

.getTimeLeft()

Returns how many milliseconds left to next tick.

const myTicker = new Ticker(1000, callback)

myTicker.start()

// after about two ticks and a half (2480ms)
myTicker.getTimeLeft() // --> 520

.stop()

Stop/Pause ticking.

When called, the Ticker instance calculates the time left to next tick and stores it on a timeLeft prop in case you'll want to resume ticking from exact same point.
Run .start() to resume.

const myTicker = new Ticker(1000, sayTick)

myTicker.start() // TICK!

// Time passes by.. TICK!.. TICK!..
myTicker.stop()

console.log(myTicker.timeLeft) // 680 (ms left to next tick)

// resume
myTicker.start() // next tick in 680ms.

.reset()

Reset the ticker.

Reseting a ticker doesn't change its initial interval.

Can be called whether the ticker is running or not:

  • When running: Restart as if you have just started. Doesn't stop the ticker.
  • When stopped: Reset the recorded timeLeft.
const myTicker = new Ticker(1000, sayTick)

myTicker.start() // new start point
myTicker.reset() // new start point. still running...
myTicker.stop() // save `timeLeft`

myTicker.start() // resume from the same point
myTicker.stop()  // save `timeLeft`
myTicker.reset() // reset `timeLeft`

myTicker.start() // new start point

.destroy()

Destroy the ticker.

Calls .stop() and .reset() and set the .isOk prop to false.

To use the same Ticker instance again, isOk should be set to true.

const myTicker = new Ticker(1000, sayTick)

myTicker.start()
myTicker.destroy()

// Resurrection
myTicker.isOk = true;

myTicker.start()

Playground / benchmark


Compares Ticker with using vanilla setTimeout & setInterval

$ npm run playground