Giter VIP home page Giter VIP logo

stopwatch.js's Introduction

Stopwatch.js

Stopwatch.js is a JavaScript utility that mimics the functions of a common stopwatch. Specifically, it provides mechanisms to start, pause, and stop, and allows you to execute one or more code snippets at specified time intervals.

Stopwatch.js runs in the browser and on the server using Node.js.

Demonstration

Installation

Download Stopwatch.js to your project or install using Bower (bower install stopwatch). Then, include the script either through a <script> tag in your document, or via RequireJS.

Script

<script src="/path/to/libs/Stopwatch.js"></script>

RequireJS

define([
  'path/to/libs/Stopwatch.js'
], function(Stopwatch) {
  var stopwatch = new Stopwatch();
});

Usage

Create an instance of a Stopwatch using new:

var timer = new Stopwatch();

Start a Stopwatch using the start function:

var timer = new Stopwatch();
timer.start();

Pause or stop a Stopwatch using the pause or stop functions. Paused Stopwatches will maintain the previous elapsed time and will resume from that duration if restarted in the future.

var timer = new Stopwatch();
timer.start();
// ... Later in your script
timer.pause(); // or timer.stop();

Get the current elapsed time in milliseconds using the getElapsed function, or as a formatted string using toString. Thanks to type coersion in JavaScript, you can simply concatenate a Stopwatch instance to a string to invoke the toString method.

var timer = new Stopwatch();
var milli = timer.getElapsed();
console.log(milli + ' milliseconds have elapsed.');
console.log('The Stopwatch has been running for ' + timer);

Finally, you can execute code at regular intervals using the onTick function, passing a function as the first parameter and a delay as the second parameter (in milliseconds). Omitting the second parameter will execute your code at a frequency of once per second.

var timer = new Stopwatch();
timer.onTick(function() {
  console.log('5 seconds have elapsed.');
}, 5000);

Resume time tracking of your tasks, in a simple way with setStartTime method, passing the time as a parameter in the format '00:00:00'.

// code...
var task = db.get_task();
var timer = new Stopwatch();
timer.setStartTime(task.time);  //time format value '00:00:00' typeof String
timer.start()

Testing

Current build status is available on Travis CI.

To run enclosed unit tests locally, clone the repository and install package dependencies, then run npm test:

$ git clone https://github.com/aduth/correctingInterval.git
$ cd correctingInterval
$ npm install
$ npm test

License

Copyright 2014 Andrew Duthie

Released freely under the MIT license (refer to LICENSE.txt).

stopwatch.js's People

Contributors

aduth avatar victordanilo 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.