brendonbribeiro / tempojs

Smart javascript timer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status npm version

tempojs

tempojs is a JavaScript library for building smart timers

  • Precise: You will not get broken values like 999ms when you were waiting for 1s
  • Configurable: You have several options to create your timer

Installation

tempojs is available on npm

npm install tempojs

And you may get it from rawgit

<script type="text/javascript" src="https://cdn.rawgit.com/brendonbarreto/tempojs/master/dist/tempo.min.js"></script>

Usage

If you're using node, first require tempojs

const Tempo = require("tempojs");

That's all you need to get it working. To test if everything is fine, execute this small example

let timer = new Tempo({
  onInterval:(time) => {
    console.log(time.toString());
  }
});

timer.start();

Time Object

tempojs uses a more human representation of time as an object. This object will be used at some points, named timeObj, and it looks like this:

{
  milliseconds: 1,
  seconds: 1,
  minutes: 1,
  hours: 1
}

empty

The term empty timeObj refers to {}, or:

{
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0
}

toString()

converts a timeObj into string (hh:mm:ss:fff). { hours: 2, minutes: 30, seconds: 45, milliseconds: 500 } will return 02:30:45:500

Settings

You can configure your timer, by passing a object as first argument when creating a new Tempo instance

let timer = new Tempo(settings);

interval

  • Represents the delay between timer ticks.
  • Default value: { seconds: 1 }
let timer = new Tempo({
  interval:{
    minutes: 1,
    seconds: 30
  }
});

On this example timer will call onInterval every 1 minute and 30 seconds after started. It will also, search for onIntervals and onSpecificTimes.

decreasing

  • Determines if the timer will increase or decrease
  • Default value: false

If set to true, timer will end at endTime or empty timeObj

startTime

  • Represents the time that your timer will start.
  • Default value: empty timeObj

endTime

  • Represents the time that your timer will end.
  • Default value: empty timeObj

Timer functions

Function Description
start starts the timer on startTime or last pause time, and call onStart
pause pause the timer and call onPause
stop stop the timer, current time return to startTime, and call onStop
end current time return to startTime, and call onEnd. The difference between stop and end, is that end is called automatically when timer reaches endTime (or empty timeObj if decreasing = true)

About

Smart javascript timer

License:MIT License


Languages

Language:JavaScript 100.0%