Xenoveritas / js-timer

JavaScript library for creating countdown timers (or clocks or really anything of that nature)
MIT License
16 stars 6 forks source link

Clock module

A simple JavaScript module for generating countdown timers or clocks. Check out demo.html for a brief demo of creating a simple clock in the browser and demo.js for a demo of using this in Node.js.

This module is written to deal with common issues that come up with writing a clock that updates at a consistent rate. Looking at the JavaScript API, it would appear that setInterval would be perfect for implementing a clock that updates every second.

It is not, for a very simple reason: setInterval does not invoke the callback at anywhere near a steady rate. It is allowed to call it early, and it is allowed to call it late. An interval of 1000 may, in fact, be invoked every 950-980ms, which for a clock, leads to a clock that's about a second slow until it suddenly jumps forward a second.

This module solves that by instead using setTimeout and re-invoking it based on how long it is to the next second. It also "rounds up" the current time, ensuring that if the clock is called a few milliseconds early, it will still display for the time it's "supposed" to. This means that each tick may be slightly late or slightly early, but it will update at close-enough to once a second.

The other issue is that timeouts in a browser (through either setInterval or setTimeout) may be delayed for a variety of reasons. For example, if the OS is overloaded, or the computer is suspended, the timeout may not be invoked until well after the time. By re-invoking setTimeout based on when it was, in fact, called, this allows the clock to "skip ahead" and ignore any times it missed.

Note that if you're looking to create a clock with a smooth second hand, this is not the module you're looking for. Instead check out requestAnimationFrame.

Clock class

Very basic clock class that calls its ontick method every second.

Member Functions

Static Functions

Clock.Interval class

An interval of time - sort of like a Date() but for different periods of time.

Clock.Interval(millis) - create a new interval for the given number of milliseconds

Member variables

Building

Building the source is currently done using Yarn and Grunt. This makes building the code simply:

yarn
grunt