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

chronometer.js

v1.0.3

Published

A Javascript chronometer object, that uses ObjectEventTarget to trigger it events

Downloads

3

Readme

chronometer.js

This is just a chronometer constructor in JavaScript, that uses ObjectEventTarget as prototype to support events listeners.

DEMO PAGE

Motivation

I want to create a demo how to use the ObjectEventTarget and inspire other JavaScript programmers to use prototype.

Nothing better then a project that evolves a logical part that can be totally separated from the design, and also have it owns events. This will allow you do a lot of things, so you also can prototype your function with the chronometer, creating a more sophisticate object.

How to use

You just need to create a instance or more using new Chronometer(), then you are good to go.

Methods

Public methods:

  • start(): Start your chronometer
  • stop(): Stop your chronometer (next time you start, it will reset the properties)
  • pause(): It will pause your chronometer, adding a step to it, the time paused is not computed as elapsedTime, you can call start() to continue tracking the time
  • step(): It also create a step, but will not pause, very useful if you want to track laps
  • update(): It will update the values of the elapsedTime from global and current step.
  • isRunning(): Return true when it's running
  • isPaused(): Return true when it's paused
  • isStopped(): Return true when it's stopped

Internal use methods:

  • createStep( forceTime ): Return a ChronometerStep instance, if forceTime is undefined, it will use Date.now() as value of pausedTime
  • setState(): Change the state value, when you start, pause or stop.
  • autoUpdate(): Create a internal recursive timeout to trigger update based on updateInterval value.

Properties

** Read-write properties:**

  • updateInterval: number (default: 50) milliseconds between each time update will be triggered by the autoUpdate.

** Read-only properties:

  • state: Define the chronometer state using the constants: RUNNING, PAUSED or STOPPED.
  • startTime: Timestamp with register when chronometer start running.
  • stopTime: Timestamp with register when chronometer stop running.
  • steps: array with the ChronometerStep instances for each time you pause or step.
  • elapsedTime: number in milliseconds of the elapsed time from when you started the chronometer, will be updated by any action or autoUpdate.

Internal use properties:

  • autoUpdateID: Store the last autoUpdate timeout instance ID.

Events

Events are provided by ObjectEventTarget prototype, means that you can use addEventListener, removeEventListener or dispatchEvent in any instance of chronometer.

Cancelable Events:

  • start: Dispatched when is about to start the chronometer.
  • stop: Dispatched when is about to stop the chronometer.
  • step: Dispatched when a step is about to be pushed.
  • update: Dispatched when is about to update the chronometer properties and it steps.

Non-cancelable Events:

  • started: When the chronometer has been started.
  • stopped: When the chronometer has been stopped.
  • stepinserted: Every time a step is pushed in the steps array.
  • updated: Every time that the chronometer properties are updated (good if you want to update your DOM)
  • state: When a state is changed (the event contains a detail property with the old state value).

Constants

  • STOPPED
  • RUNNING
  • PAUSED

Prototype

To prototype you can call from your new constructor passing your context.

Example:

function FancyChronometer( name ){
    Chronometer.call(this);
    this.name = name;
}
FancyChronometer.prototype = Chronometer.prototype;

Architecture

There is no private methods, except by the instance ChronometerStep that only can be accessed by createStep. The thing is, you have freedom to do whatever you want, even destroy the chronometer.

Object.defineProperty isn't been used to protect the read only properties, because I want to be backward compatible with ES3 and because it's more about good sense to don't change it values, but if you need for any misterious reason, you can change it.

autoUpdateID is an common example of how prototypes mean to be used if you want to give abbility to prototype any new constructor. If you want to hide it value from the other programmers, the only way is to use a WeakMap for that, and keep the methods that depends on it in the prototype, or create the method autoUpdate with a closure inside the constructor, but it's more code, memory mess just to protect other programmers from doing mistake with it. Doesn't worth.

If your browser has support to Object.defineProperties, it will add all methods as not enumerable, if you do a for in, they will not be iterated. But if you plan to give support to IE-9, use hasOwnProperty or a shim to ensure that behaviour.

Files

  • chronometer.js: Chronometer constructor and prototype logic.
  • demo.js: Used by index.html to create a demo biding in the DOM.