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

clocky

v0.6.0

Published

Call functions at various intervals

Downloads

10

Readme

Clocky Build Status Coverage Status JavaScript Style Guide MIT licensed

Clocky executes callbacks at predefined intervals called ticks, providing an easy-to-use wrapper around the built in setInterval method.

Installation

Simplest option, with npm:

npm install clocky

or, if you prefer old fashioned downloads, click here for the minified UMD file

Usage

Javascript

var Clocky = require('clocky')

var clocky = new Clocky({
  onTick: function (ticks, startedAt, elapsed) {
    console.log('ticked ' + ticks + ' times')
    console.log('started at ' + startedAt)
    console.log('total elapsed time (including pauses):' + elapsed + ' seconds')
  }
  tickEvery: 2 // seconds
})

clocky.start()

ECMA2015

import {Clocky} from 'clocky'

let clocky = new Clocky({
  onTick: (ticks, startedAt, elapsed) => {
    console.log(`ticked ${ticks} times`)
    console.log(`started at ${startedAt}`)
    console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
  },
  tickEvery: 2 // seconds
})

clocky.start()

API

Clocky provides a fluent interface which allows for chaining methods. The methods outlined below can also be invoked in the constructor as an object. The following examples are equivalent:

let clocky = new Clocky({
  onTick: (ticks, startedAt, elapsed) => {
    console.log(`ticked ${ticks} times`)
    console.log(`started at ${startedAt}`)
    console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
  },
  tickFor: 5,
  tickEvery: 2 // seconds
})

clocky.start()

and

let clocky = new Clocky()

clocky
  .tickFor(5)
  .tickEvery(2)
  .onTick((ticks, startedAt, elapsed) => {
    console.log(`ticked ${ticks} times`)
    console.log(`started at ${startedAt}`)
    console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
  })
  .start()

State

Clocky has 3 states or statuses:

  • stopped - the internal timer is stopped, there is no internal state and no events are fired
  • running - the internal timer is started and it emits ticks at the predefined intervals
  • paused - the internal timer is stopped and no events are fired, but the internal state is maintained (number of ticks elapsed, etc.)

In order to provide consistency, methods that alter the behavior (tickEvery(), runFor()) can only be called if the status is "stopped"

runFor()

Specify the amount of time Clocky needs to run for. After this amount has elapsed, Clocky will automatically stop and no further ticks will be fired. This method can only be called when the status is stopped

clocky.runFor(5) // seconds

tickEvery()

Specify the amount of time in seconds between 2 consecutive ticks. Must be called when clocky is "stopped"

clocky.tickEvery(5) // seconds

tickOnResume()

Specify a truthy value is Clocky needs to tick when it's resumed.

tickOnStart()

Specify a truthy value is Clocky needs to tick when it's started.

on()

Shorthand method for adding event. Specify the event as a string.

clocky.on('tick', () => {
  console.log('ticked')
})

onStart()

Provide a callback to be executed when the timer is started.

clocky.onStart((ticks, startedAt, elapsed) => {
  console.log(`ticked ${ticks} times`)
  console.log(`started at ${startedAt}`)
  console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
})

onPause()

Provide a callback to be executed when the timer is paused. This event is triggered before the pause.

clocky.onPause((ticks, startedAt, elapsed) => {
  console.log(`ticked ${ticks} times`)
  console.log(`started at ${startedAt}`)
  console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
})

onResume()

Provide a callback to be executed when the timer is resumed. This event is triggered after the pause.

clocky.onResume((ticks, startedAt, elapsed) => {
  console.log(`ticked ${ticks} times`)
  console.log(`started at ${startedAt}`)
  console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
})

onStop()

Provide a callback to be executed when the timer is stopped.

clocky.onStop((ticks, startedAt, elapsed) => {
  console.log(`ticked ${ticks} times`)
  console.log(`started at ${startedAt}`)
  console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
})

onTick()

Provide a callback

clocky.onTick((ticks, startedAt, elapsed) => {
  console.log(`ticked ${ticks} times`)
  console.log(`started at ${startedAt}`)
  console.log(`total elapsed time (including pauses): ${elapsed} seconds`)
})

start()

Start the timer. If the timer is not stopped, it will throw an exception.

pause()

Pause the timer. Pausing will also stop the timer for the "runFor()" method, so that Clocky instance with a runFor() interval of 10 seconds paused in the middle for 5 it will actually run for 15 seconds.

resume()

Resume a previously stopped timer. Will throw an exception if the timer is not paused

stop()

Stop timer and reset it to its initial state. Can be trigger regardless of the status.

getStatus()

Return the current status, can be one of "stopped", "running" or "paused"

isStopped(), isRunning(), isPaused()

Return boolean values based on the current status