GithubHelp home page GithubHelp logo

bogdan-kalynovskyi / correctinginterval Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aduth/correctinginterval

0.0 0.0 0.0 201 KB

Provides setCorrectingInterval and clearCorrectingInterval to act as an auto-correcting alternative to setInterval

License: MIT License

JavaScript 98.47% CSS 1.53%

correctinginterval's Introduction

Build Status

correctingInterval

correctingInterval exposes two functions, setCorrectingInterval and clearCorrectingInterval, which serve as drop-in replacements for setInterval and clearInterval. Unlike setInterval, the setCorrectingInterval function automatically adjusts to correct subsequent intervals that were delayed due to latency. This is useful if your delayed executions need to occur at predictable intervals. For more information, refer to my related blog post below:

Creating a self-correcting alternative to JavaScript's setInterval

Usage

Browser

Download correctingInterval.js to your project or install using Bower (bower install correctingInterval). Include the file using a <script> tag. Then, use with the same syntax as you would use setInterval, e.g.:

var startTime = Date.now();
setCorrectingInterval(function() {
  console.log((Date.now() - startTime) + 'ms elapsed');
}, 1000);

RequireJS

Download correctingInterval.js to your project or install using Bower (bower install correctingInterval). Include the file as a dependency to your module. correctingInterval is passed as an object containing the two functions as object members, e.g.:

define([
  'vendor/correctingInterval'
], function(ci) {
  var startTime = Date.now();
  ci.setCorrectingInterval(function() {
    console.log((Date.now() - startTime) + 'ms elapsed');
  }, 1000);
});

Node.js

Install correctingInterval using npm (npm install correcting-interval). Require correctingInterval in your application script. correctingInterval is passed as an object containing the two functions as object members, e.g.:

var ci = require('correcting-interval');

var startTime = Date.now();
ci.setCorrectingInterval(function() {
  console.log((Date.now() - startTime) + 'ms elapsed');
}, 1000);

Clearing Intervals

Similar to setInterval, you can stop running intervals by storing a reference to the value returned by setCorrectingInterval and later use as the sole parameter to clearCorrectingInterval, e.g.:

var intervalId = setCorrectingInterval(function() { }, 1000);

// Later in your script...
clearCorrectingInterval(intervalId);

License

Copyright 2014 Andrew Duthie.

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

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.