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

tm-timer

v1.0.2

Published

A simple countdown Timer class (no GUI)

Downloads

24

Readme

TM-Timer

License: MIT Build Status

A simple count-down Timer class, based on TM-Ticker (no GUI)

Installation

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

TL;DR

Jump to API

Create:

// construct & config
const t = new Timer(duration, finalCallback)

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

Config:

t.set(duration)
t.whenDone(finalCallback)
t.onTick(tickHandler)

Use:

now is optional

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

API


Constructor

const t = new Timer(duration, finalCallback);
  • duration [number, optional*]
    * Required to start. optional at construction.
    Countdown in milliseconds. Pass null to set the callback alone.

  • finalCallback [function, optional]
    The function you want to call when countdown is over.

Example:

const fiveMinutes = 5 * 60 * 1000;

const t = new Timer(fiveMinutes, () => {
    console.log('Game Over');
});

Configuration

A Timer instance won't tick unless it has a duration.

You can set the timer's duration and final callback on construction or later with the following methods which are very self explanatory:

const myTimer = new Timer();

myTimer.set(fiveMinutes)
myTimer.whenDone(finalCallback)
myTimer.onTick(myTickHandler)

.set(duration)

  • duration [number, required]
    Set the total time in milliseconds to count down to.
const fiveMinutes = 5 * 60 * 1000;

myTimer.set(fiveMinutes);

.whenDone(finalCallback)

  • finalCallback [function, required]
    Runs when timer finishes. Gets no arguments.
myTimer.whenDone(() => {
    console.log("Time's Up!");
});

.onTick(callback)

As the timer counts down, it ticks every 500ms (twice every second).
Use when you can bind a tick handler function.
The first tick happens on start, synchronously, before any timeout.

  • callback [function, optional]
    The callback function recieves two arguments:
    1. isBigTick [boolean]
      Equals true on "big" ticks (a whole second: 0, 1000, 2000, 3000 etc.)
      Equals false on "small" ticks (half a second: 500, 1500, 2500, 3500 etc.)
      For example, you could update your clock's digits on big ticks and blink the clock's colons on small ticks (04:20).

    2. timeLeft [number]
      Time left in milliseconds.

Example:

myTimer.onTick((isBigTick, timeLeft) => {
    console.log(isBigTick); //  | true | false | true | false |...
    console.log(timeLeft);  //  | 5000 | 4500  | 4000 | 3500  |...

    /*
    * NOTE:
    * The following functions are made up and are not part of TM-Timer.
    */
    if (isBigTick) {
        updateClock(timeLeft)
        showColons() // (04:20)
    }
    else {
        hideColons() // (04 20)
    }
});

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 counting down.

Calls the first tick (if a tick handler is set with .onTick(fn)).
When called after a .stop() it acts as a "resume" function. There will be no start-tick in this case.

// optional
const timestamp = Date.now()

myTimer.start(timestamp)

.getTimeLeft()

Returns how many milliseconds left.

const myTimer = new Timer(3000, callback)

myTimer.start()

// after 2480 ms (for example)
myTimer.getTimeLeft() // --> 520

.stop()

Stop/Pause counting down.

Run .start() to resume.

const myTimer = new Timer(fiveMinutes, gameOver)

myTimer.start()

// Take a break
myTimer.stop()

// Resume
myTimer.start()

.reset()

Reset the countdown with full original duration.

Can be called whether the timer is running or not. When called while running, it acts like a "restart" and doesn't stop the timer.

const myTimer = new Timer(fiveMinutes, gameOver)

myTimer.start()

/* after 2 minutes */
myTimer.reset() // re-start counting down five minutes. No stop.
const myTimer = new Timer(fiveMinutes, gameOver)

myTimer.start()

/* after 2 minutes */
myTimer.stop()
myTimer.reset()
myTimer.start() // start counting down five minutes.

.destroy()

Destroy the timer. Removes duration and bound callbacks.

Cannot be used again unless re-configured.

const myTimer = new Timer(fiveMinutes, gameOver)

myTimer.start()
myTimer.destroy()

myTimer.set(fiveMinutes)
myTimer.whenDone(gameOver)

Playground


$ npm run playground