Giter Site home page Giter Site logo

correctingtimer's Introduction

CorrectingTimer

A self-correcting JS timer. Replacement for setInterval.

##Boring stuff Timer v.0.8.2 ~ Copyright (c) 2012 Luke Moody, http://github.com/SquareFeet

Released under MIT license.

##Usage

// As a constructor:
var myTimer = new Timer(function(drift) {
    // Do stuff here. drift arg is Number.
}, 100);
myTimer.start();

// As a replacement for setInterval
var myTimer = setTimer(function(drift) {
    // Do stuff here. drift arg is Number.
}, 100);

See code comments for a few more useful goodies...

##Pointless Explanation

Since JS timers (setTimeout/setInterval) are useless at adhering to their specified duration, this module calculates the positive or negative drift of a timer and auto-corrects it.

As an example, you write this code, and run it:

var prev = Date.now();
setInterval(function() {
    console.log(Date.now() - prev);
    prev = Date.now();
}, 50);

The chances are, what is logged will rarely be 50, especially if other operations are going on in the background on the same thread/worker.

Using this module with the following code will yeild different results:

setTimer(function(drift) {
    console.log(Date.now() - prev, drift);
    prev = Date.now();
}, 50);

What will get logged now will probably still not be 50. However, lets assume that it logs 52 the first time round. The timer will then set a timeout for the next tick to be 48ms, since the first time round was delayed by 2ms.

For safety's sake (should I remove this?) it allows a minimum timeout duration of 10ms.

correctingtimer's People

Contributors

squarefeet avatar sijones-uk avatar

Watchers

 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.