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

timer.js

v1.0.4

Published

Simple and lighweight but powerfull eventdriven JavaScript timer

Downloads

2,012

Readme

Timer.js

Build Status

Simple and lightweight library without any dependencies to create and manage, well, timers.

Demo

Installation

The easiest way to install timer.js is via npm:

$ npm install timer.js

or if you prefer good old files, you can manually download dev or min versions.

Examples

Let's cook pizza

var pizzaTimer = new Timer();
var pizzaCookingTime = 15 * 60; // 15 minutes

timer.start(pizzaCookingTime).on('end', function () {
  alert('Pizza is ready, bon appetit!');
});

Usage

Timer.js is written in UMD style, so it's compatible with AMD(Require.js), CommonJS(nodejs, browserify, etc.) and direct browser usage as a global.

API

All methods listed below support chaining, so you can write:

myTimer.start(10).on('pause', doSmth).pause(); // and so on

Also you can use this keyword inside of methods as a reference to the instance of Timer

initialization

To create Timer with specific event handlers and options you can pass them as argument to constructor

var myTimer = new Timer(options);

list of available options:

  • ontick - what to do on every tick
  • tick - set specific tick(e.g. you can set it to 2, then your ontick handler will fire every 2 seconds)
  • onstart - start event handler
  • onstop - stop event handler
  • onpause - pause event handler
  • onend - end event handler(when Timer stops without interrupt)
var myTimer = new Timer({
  tick    : 1,
  ontick  : function(sec) { console.log(sec + ' seconds left') },
  onstart : function() { console.log('timer started') },
  onstop  : function() { console.log('timer stop') },
  onpause : function() { console.log('timer set on pause') },
  onend   : function() { console.log('timer ended normally') }
});

.start(time)

starts a Timer for a specified time

myTimer.start(10) // start a timer for 10 seconds

.pause()

pause timer

myTimer.pause()

after pause you can continue the job by myTimer.start()

.stop()

to stop timer doing his job

myTimer.stop()

.on(option, function)

set some specific option, support options without 'on' prefix. Available options are : tick, ontick, start, onstart, end, onend, stop, onstop, pause, onpause

myTimer.on('end', function() {
  console.log('woo-hooo! my timer ended normally')
})

.off()

similar to 'on()' but it will remove handler

myTimer.off('pause')

.options()

define multiple specific options at once as an object

myTimer.options({
    onend : function() {
        console.log('onend')
    },
    ontick : function() {
        console.log('every tick');
    }
})

You can use .off('all') to restore all previously defined options to defaults

myTimer.off('all')

.getStatus()

get current status of timer. Available statuses are: 'initialized', 'started', 'paused', 'stopped'

myTimer.getStatus() // 'initialized'
myTimer.start(20).getStatus() // 'started'
myTimer.pause().getStatus() // 'paused'

.getDuration()

get remaining time(in ms)

myTimer.start(20)
// some operations that lasts for 2 seconds
myTimer.getDuration() // 18000

.measureStart(label)

Start a high-performance measurement with an associated label, you need to use the same label to stop measurement, so make sure you've saved it

.measureStop(label)

Stop the measument with the associated label, returns the numbers of elapsed ms

Example:


myTimer.measureStart('label1');
var a = [];
for (var i = 10000000; i >= 0; i--) {
    a.push(i * Math.random());
};
myTimer.measureStop('label1'); // 276 i.e.

Note! '' (empty string) equals to absence of argument, and it is valid

timer.measureStart();
//some operations
timer.measureStop();

will work

Tests

Running tests is pretty straightforward

$ npm test

Tests are written with Jasmine, you can find all specs in test/specs folder.

Contributing

If you've found a bug, something is not working as it shoud be or you came up with some new cool feature, feel free to create an issue here or send a pull request.

Changelog