GithubHelp home page GithubHelp logo

xadn / tick-tock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from unshiftio/tick-tock

1.0 2.0 0.0 109 KB

Timer management, never forget to clear timers again.

Home Page: http://unshift.io

License: MIT License

JavaScript 100.00%

tick-tock's Introduction

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

tick-tock's People

Contributors

3rd-eden avatar lpinca avatar xadn avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.