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

awesome-countdown-timer

v0.0.4

Published

An awesome javascript timer

Downloads

8

Readme

Awesome Countdown Timer

The Awesome Countdown Timer is a simple, efficient JavaScript library designed to implement countdown timers seamlessly within your projects.

Installation

Install the Awesome Countdown Timer via npm:

npm install awesome-countdown-timer

Demo

TBD

Getting Started

Creating a Countdown Timer

To create a new countdown timer, use the createCountdownTimer function, which accepts a configuration object:

import { createCountdownTimer } from 'awesome-countdown-timer';

const timer = createCountdownTimer({
  startTime: 10, // 10 seconds countdown timer
  onTick: (event) => console.log(event), // onTick is invoked every second when timer is running
});

Starting the Countdown Timer

Start the countdown timer using startTimer

timer.startTimer();

Pausing Countdown Timer

Pause the countdown timer using pauseTimer

timer.pauseTimer();

Finishing Timer

Invoke finishTimer to set the current time to 0.

timer.finishTimer();

Resetting Timer

Reset the timer to its initial state using resetTimer.

timer.resetTimer();

Getting Current State

Retrieve the current timer state using getInfo.

const info = timer.getInfo();
console.log(info);

Documentation

Countdown Timer Configuration

The createCountdownTimer function accepts a CountdownTimerConfig object with the following properties:

type CountdownTimerConfig = {
  startTime: number;
  disableInvalidStateTransitionError?: boolean;
  onCreate?: (event?: CountdownTimerEvent) => void;
  onStart?: (event?: CountdownTimerEvent) => void;
  onPause?: (event?: CountdownTimerEvent) => void;
  onTick?: (event?: CountdownTimerEvent) => void;
  onFinish?: (event?: CountdownTimerEvent) => void;
  onReset?: (event?: CountdownTimerEvent) => void;
};

| field | description | type | | ---------------------------------- | ------------------------------------------------------------- | -------- | | startTime | Duration of the timer in seconds | number | | disableInvalidStateTransitionError | Disables throwing error when invalid state transition happens | boolean | | onCreate (optional) | Event handler for create event | function | | onStart (optional) | Event handler for start event | function | | onPause (optional) | Event handler for pause event | function | | onTick (optional) | Event handler for tick event | function | | onFinish (optional) | Event handler for finish event | function | | onReset (optional) | Event handler for reset event | function |

Countdown Timer States

The countdown timer can be in the following states:

| state | description | | -------- | -------------------------------------------------------------------------------------- | | idle | Countdown timer is not started yet. | | running | Countdown timer is currently running. The timer will trigger tick event every second | | paused | Countdown timer is paused. | | finished | Countdown timer has reached 0 seconds and finished. |

Countdown Timer API

The countdown timer object created by createCountdownTimer includes the following properties and methods:

export interface ICountdownTimer {
  id: TimerID;
  addEventListener: (
    type: CountdownTimerEventType,
    handler: CountdownTimerEventHandler
  ) => void;
  startTimer: () => ICountdownTimer;
  pauseTimer: () => ICountdownTimer;
  finishTimer: () => ICountdownTimer;
  resetTimer: () => ICountdownTimer;
  getInfo: () => CountdownTimerInfo;
}

| property | description | | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | id | Unique id of the countdown timer | | addEventListenr | Attach event listener to CountdownTimerEventType events | | startTimer | Start (if state is idle) or resume (if stats is paused) countdown timer | | pauseTImer | Pause countdown timer | | finishTimer | Forcefully finish countdown timer. This will make the state finished and make the current time to 0 | | resetTimer | Reset timer to it's initial state. It will change the state to idleand current time to whatever startTime it was given in the CountdownTimerConfig | | getInfo | Get current information about the countdown timer |

Countdown Timer States

type CountdownTimerState = 'idle' | 'running' | 'paused' | 'finished';

State Transition

The countdown timer changes states based on specific API calls, transitioning between idle, running, paused, and finished states.

| Situation | State transition | | ---------------------- | -------------------------------------- | | Create countdown timer | countdown timer initializes to idle | | Reset countdown timer | any state → idle | | Start countdown timer | idle | pausedrunning | | Pause countdown timer | runningpaused | | Finish countdown timer | idle|paused|runningpaused |

Invalid State Transition

When disableInvalidStateTransitionError is enabled in CountdownTimerConfig, invalid transition will throw error.

The following state transitions are invalid.

| Invalid state transition | | ------------------------ | | runningrunning | | finishedrunning | | idlepaused | | pausedpaused | | finishedpaused |

Countdown Timer Events and Event Handlers

Countdown Timer Events

The countdown timer fires events such as create, start, pause, finish, tick, and re`set, triggering respective event handlers when attached.

| Event type | Description | Event listener | | ---------- | ------------------------------------------------------------- | -------------- | | create | Fired when countdown timer is created |onCreate | |start | Fired when countdown timer starts |onStart | |pause | Fired when countdown timer pauses |onPause | |finish | Fired when countdown finishes |onFinish | |tick | Fired every second when countdown timer is inrunningstate |onTick | |reset | Fired when countdown timer resets |onReset` |

Event Handlers

When an event fires, corresponding event handler will be invoked.

Event handlers can be attached in two ways:

  1. Passing Event Handlers When Creating Countdown Timer
  2. Dynamically Attaching Event Handlers

Passing Event Handlers When Creating Countdown Timer

const timer = createCountdownTimer({
  startTime: 10,
  onCreate: (event) => {
    console.log(event);
  },
  onStart: (event) => {
    console.log(event);
  },
  onTick: (event) => {
    console.log(event);
  },
  onPause: (event) => {
    console.log(event);
  },
  onFinish: (event) => {
    console.log(event);
  },
  onReset: (event) => {
    console.log(event);
  },
});

Dynamically Attaching Event Handlers

Event handlers can be dynamically attached using the addEventListener.

timer.addEventListener('create', (event) => {
  console.log(event);
});

Technologies used

Web Worker

awesome-countdown-timer utilizes Web Worker to ensure accurately update time in the background thread (not the main thread).

Webpack

awesome-countdown-timer is bundled using Webpack.

Typescript

awesome-countdown-timer provides type declaration files for those who are integrating awesome-countdown-timer to a Typescript project.