GithubHelp home page GithubHelp logo

timer.js's Introduction

Timer.js Logo

What is it?

Timer.js is a periodic timer for Node.js and the browser.

Overview

Creating a Timer

A new Timer is created with either the new operator:

var timer = new Timer(resolution);

or the factory method:

var timer = Timer(resolution);

Where resolution is the tick rate of the timer. If resolution is omitted it defaults to 1000 milliseconds. resolution can be either an integer in milliseconds or a string describing the resolution such as '2 seconds' or '3 hours, 20 minutes'.

Binding and unbinding events to the Timer with bind() and unbind()

Events can be bound to the timer with the bind(when, callback) method:

timer.bind(1000 * 3, function () {}); // fire event after 3 seconds

when can also be a string describing the moment on that the event should be fired:

timer.bind('3 seconds', function () {});

wherein when the event is fired is limited through the timer resolution.

For unbinding unbind(callback) can be used.

Binding shortcuts every() and after()

every(when, callback) is just an alias for bind(when, callback), to make code more readable:

timer.every('2 seconds', function () {});

after(when, callback) is also binds an callback to the timer, but unbinds it automatically after the event is fired:

timer.after('5 seconds', function () {});

Instance methods

timer.start() start the timer.

timer.stop() stop the timer.

timer.reset() stops the timer and resets the tick count.

timer.clear() resets the timer and ubinds all event listeners.

timer.ticks() returns the tick count since start.

timer.resolution() returns the timer resolution in milliseconds.

timer.running() indicates if the timer is running.

Usage example

var timer = new Timer('100 milliseconds');

timer.every('6 seconds', function () {
    console.log('timer event every 6 seconds');
});

timer.every('1 minute, 20 seconds', function () {
    console.log('timer event every 1 minute, 20 seconds');
});

timer.start();

Valid time strings units

millisecond(s), ms, second(s), sec(s), s, minute(s), min(s), m, hour(s), hr(s), h, day(s), d

timer.js's People

Contributors

avk avatar twipped avatar fschaefer avatar janhartigan avatar

Watchers

James Cloos 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.