Giter VIP home page Giter VIP logo

easytimer.js's Introduction

easytimer.js

NPM

Easy to use Timer/Chronometer/Countdown library compatible with AMD and NodeJS

Install

NPM

npm install easytimer.js --save

Bower

bower install easytimer.js

Library Load

Script

<script src="lib/easytimer/dist/easytimer.min.js"></script>
<script>
    var timerInstance = new easytimer.Timer();
</script>

Node

var Timer = require('easytimer.js').Timer;
var timerInstance = new Timer();

// or 

var { Timer } = require('easytimer.js');
var timerInstance = new Timer();

ES6 / Typescript Imports

import Timer from 'easytimer.js';
const timer = new Timer();

// or 

import { Timer } from 'easytimer.js';
const timer = new Timer();

AMD

require(['node_modules/easytimer.js/dist/easytimer.min.js'], function (easytimer) {
    var timer = new easytimer.Timer();
});

// or

require(['node_modules/easytimer.js/dist/easytimer.min.js'], function ({ Timer }) {
    var timer = new Timer();
});

Examples

http://albert-gonzalez.github.io/easytimer.js/

Basic Usage

var timer = new Timer(/* default config */);
timer.start(/* config */);
timer.addEventListener('secondsUpdated', function (e) {
    $('#basicUsage').html(timer.getTimeValues().toString());
});

Timer instance API

  • start(config): Start the timer
    • Parameters:
      • config (object):
        • startValues (object or array): The initial values of the timer when it is started
          • Accepted Values: Object with some of these keys: 'secondTenths', 'seconds', 'minutes', 'hours', 'days'. Or an array with this 5 positions: [secondTenths, seconds, minutes, hours, days]
          • Default Value (object or array): [0,0,0,0,0]
        • target (object or array): The target values that will make the timer to stop.
          • Accepted Values: Object with some of these keys: 'secondTenths', 'seconds', 'minutes', 'hours', 'days'. Or an array with this 5 positions: [secondTenths, seconds, minutes, hours, days]
          • Default Value: Undefined
        • precision (string): The timer update frequency.
          • Accepted Values: 'secondTenths', 'seconds', 'minutes', 'hours'
          • Default value: 'seconds'
        • callback (function): A callback function executed every time the timer is updated. The callback receives the timer as a parameter.
        • countdown (bool): If true, the timer is a countdown.
    • You can pass this same config when a Timer instance is created. For example: new Timer({ target: { seconds: 10 } }). This default config will be merged with the config passed to the start function.
  • stop(): Stop the timer
  • pause(): Pause the timer. After pause, you can call the start method without parameters to resume the timer.
  • reset(): Reset the timer values
  • isRunning(): Returns true if the timer is currently running.
  • addEventListener(eventType, callback): Add a listener to an event. Timer triggers events when is updated
    • Events triggered:
      • secondTenthsUpdated
      • secondsUpdated
      • minutesUpdated
      • hoursUpdated
      • daysUpdated
      • targetAchieved
      • stopped
      • reset
      • started
      • paused
    • Parameters:
      • eventType (string): The type of the event that you want to listen.
      • callback (function): Function that will be executed when the timer triggers the specified event. The callback receives an object with the timer as a parameter.
  • on(eventType, callback): addEventListener alias.
  • removeEventListener(eventType, callback): Remove an event listener from the timer. Same usage as addEventListener.
  • off(eventType, callback): removeEventListener alias.
  • getTimeValues(): Returns an object with the current values. The keys of the returned object are 'secondTenths', 'seconds', 'minutes', 'hours' and 'days'.
  • getTotalTimeValues(): Returns an object with the current absolute values. The keys of the returned object are 'secondTenths', 'seconds', 'minutes', 'hours' and 'days'.

Browser Support

Easytimer uses dispatchEvent, and this feature is available in these Browsers:

  • Chrome (version >= 4)
  • Firefox (version >= 2)
  • IE (version >= 9)
  • Opera (version >= 9)
  • Safari (version >= 3)
  • Mobile browsers

Known Issues

This library uses the Date class to calculate timer values. Date uses the system clock. If this clock is changed to a different date while the timer is running the timer values will be incorrect after that. If you know a way to fix that without losing time precision, please collaborate!

easytimer.js's People

Contributors

albert-gonzalez avatar alexdlm avatar hayrich avatar ozdemirburak avatar herberttn avatar avadh88 avatar zakuni avatar

Watchers

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