Giter Club home page Giter Club logo

js-timer's Introduction

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

  • start() - Starts the clock. Events do not fire until start() is called.
  • stop() - Stops the clock. No more events will be called.
  • ontick(now) - Called once a second with the current time as a Date. Note that the Date given will be rounded to the nearest second - see above for an explanation as to why.

Static Functions

  • Clock.zeropad(number) - Utility function to 0-pad a two-digit number, since this comes up so often. This will take numbers 0-9 and produce strings like "01".

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

  • weeks - number of weeks in the interval
  • days - number of days in the interval
  • hours - number of hours in the interval
  • minutes - number of minutes in the interval
  • seconds - number of seconds in the interval
  • millis - number of milliseconds in the interval
  • isInPast - indicates that the interval occurred in the past (over a negative number of milliseconds)

Building

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

yarn
grunt

js-timer's People

Contributors

xenoveritas avatar dependabot[bot] 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.