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-machine-node

v1.1.2

Published

A lightweight, pause-able timer class

Downloads

41

Readme

Timer Machine

npm

A simple, flexible timer for JavaScript.

Under the hood, Node's process.hrtime() is used to calculate time between intervals. hrtime (as opposed to Date().getTime() or any date/time based timing mechanism) are not subject to clock drift and allow for more accurate measurements. You can read this blog post for more information on process.hrtime.

A fork of the great timer-machine by Brent Burgoyne.

Installation

$ npm install timer-machine-node --save

Basic Usage

var Timer = require('timer-machine-node')
var myTimer = new Timer()

myTimer.start()
myTimer.stop()
myTimer.time() // -> time in ms

Named Timers

Timer Machine can maintain references to named timers. When the static method get('name') is called, it constructs a new instance if the name did not already exist, and returns the instance of Timer. This makes it easy to share a timer across multiple modules.

Timer.get('my').start()
Timer.get('my').time()

Alternatively, use it on the require if you only need a single named instance.

var myTimer = require('timer-machine').get('my')

Timer Machine allows for deleting named timers by calling the destroy('name') static method.

Timer.destroy('my')

Timer Controls

start()

By default, a new Timer object is stopped, unless the first argument of the constructor is true, and is started by calling the start() method. The start() method returns a Boolean indicating whether or not the timer was started.

var timer1 = new Timer()
timer1.start() // -> true - the timer started

var timer2 = new Timer(true)
timer2.start() // -> false - the timer is already started

stop()

To stop or pause a timer, call the stop() method. Similar to the start() method, stop() returns a Boolean indicating whether or not the timer was stopped.

var timer = new Timer()
timer.stop() // -> false - the timer is already stopped
timer.start()
timer.stop() // -> true - the timer stopped

A stopped Timer can be started again. The timer will only track the total length of time the timer has been started.

var timer = new Timer()
timer.start()
timer.stop()
timer.start()

toggle()

The toggle() method will call start() if the Timer is stopped or stop() if the timer is started.

var timer = new Tiemr()
timer.isStarted() // -> false
timer.toggle()
timer.isStarted() // -> true
timer.toggle()
timer.isStarted() // -> false

Time

time()

To get the length of time that a timer has run, the time() method can be used to a Number of the current time in milliseconds.

var timer = new Timer()
timer.start()
setTimeout(function () {
  timer.time() // -> ~100
}, 100)

emitTime()

The emitTime() method is the same as the time() method, except it emits a 'time' event with the current time in milliseconds. See Events below.

valueOf()

On Timer objects, valueOf() is an alias for time() and is used internally by JavaScript when a timer object is converted to a primitive value. This is useful for, among other things, adding and subtracting timers.

//...
timer1.time() // -> 50
timer2.time() // -> 30
timer1 + timer2 // -> 80
timer1 - timer2 // -> 20
timer + 0 // -> 50

toString()

To string is used by JavaScript to convert an object in to a string. The Timer toString() method returns the time() prepended with "ms".

console.log("Current time: " + timer1) // -> "Current time: 50ms"

timeFromStart()

While the time() returns the total number of milliseconds for the Timer, timeFromStart() returns only the time since the most recent start().

var timer = new Timer()
timer.start()
timer.timeFromStart() === timer.time() // -> true
//...
timer.stop()
timer.start()
timer.timeFromStart() === timer.time() // -> false

Timer State

isStarted()

A Timer object has an isStarted() method. It returns a true if the timer is started and false if it is stopped.

var timer = new Timer
timer.isStarted() // -> false
timer.start()
timer.isStarted() // -> true

isStopped()

A Timer object has an isStopped() method, which is the inverse of the isStarted() method.

var timer = new Timer
timer.isStopped() // -> true
timer.start()
timer.isStopped() // -> false

Events

A Timer object inherits from EventEmitter allowing event listeners to added an removed. A Timer emits three events.

Event: 'start'

The 'start' event is emitted every time a Timer is started, whether it be by the start() method or toggle() method.

timer.on('start', function () {
  console.log('The timer started')
})

Event: 'stop'

The 'stop' event is emitted every time a Timer is stopped, whether it be by the stop() method or toggle() method.

timer.on('stop', function () {
  console.log('The timer stopped')
})

Event 'time'

The 'time' event is only emitted when the emitTIme() method is called. The event handler callback receives the current time in milliseconds as the first argument.

var timer = new Timer()
timer.on('time', function (time) {
  console.log('Current time: ' + time + 'ms')
})
timer.start()
setInterval(timer.emitTime.bind(timer), 1000)

Development

Pull requests are welcome.

Get the code

$ git clone [email protected]:brentburgoyne/timer-machine.git

Install the dependencies

$ npm install

Run the tests

$ npm test

License

Copyright (c) 2017 Pete Droll.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.