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

tick-tock

v1.0.0

Published

Timer management, never forget to clear timers again

Downloads

4,466

Readme

tick-tock

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

tick-tock is a small timer and setTimeout management library. Nothing to fancy, but fancy enough to make your code more readable.

Installation

This library can be used with both browserify and node.js and can be installed using npm:

npm install --save tick-tock

Usage

In all example we assume that you've required and initialized the library using:

'use strict';

var Tick = require('tick-tock')
  , tock = new Tick();

All methods return this unless stated otherwise. The constructor can be initialized with 1 argument:

  1. context This is the default context in which each setTimeout or setInterval function is executed (it sets the this value). If nothing is supplied it will default to your tick-tock instance.

The following methods are available on your constructed instance:

Tock.setTimeout()

The setTimeout method adds as you might have expected.. a new setTimeout. The timeouts are stored based on the name that your provide them. If you've already stored a timer with the given name, it will add the supplied callback to the same stack so only one timer is used and they all run at the same time. Normally you would supply the setTimeout method with a number indicating long it should timeout. In this library we also support human readable strings.

tock.setTimeout('foo', function () {}, 10);

// Ran at the same point in time as the timeout above
setTimeout(function () {
  tock.setTimeout('foo', function () {}, 10); 
}, 5);

tock.setTimeout('another', function () {}, '10 minutes');

Tock.setInterval()

Exactly the same method and functionality as above but instead of only being called once, it will called at an interval.

Tock.clear()

The clear method allows you to clear every stored timeout by name. You can supply it multiple arguments (strings) to clear all given timers and if you supply 1 strings it can be comma separated list of names. If no arguments are supplied it will clear all timers in this instance.

tock.clear('foo', 'bar');
tock.clear('foo, bar'); // Same as above.
tock.clear(); // Nuke everything.

Tock.active()

Check if there's an active timer for the given name and returns a boolean.

tock.active('foo'); // true;
tock.clear();
tock.active('foo'); // false;

Tock.adjust()

There are cases where you sometimes need to update or change the interval of an setTimeout or setInterval for example in the case of a setTimeout which coordinates a heartbeat. In order to make this easier you call the .adjust method with the name of the timeout that you want to adjust and the new interval/timeout.

tock.setTimeout('heartbeat timeout', function () {});

// you recieved a new heartbeat so you want to reset or adjust the heartbeat;
tock.adjust('heartbeat timeout', '1 second');

Tock.end()

You no longer wish to interact with your instance and wants it to be fully shut down. This kills all active timers using tock.clear() and nulls the internal properties. It will return true if it's the first time it's destroyed and false if was already destroyed before. If you call any of the other methods after destruction, they will throw errors.

tock.end();

License

MIT