1.0.0 • Published 6 years ago

self-adjusting-interval v1.0.0

Weekly downloads
38
License
ISC
Repository
github
Last release
6 years ago

Self adjusting interval

Introdution

javascript's setInterval and setTimeout are lacking when trying to build an accurate timer, furthermore, javascript's nature of being idle can throw off your timers even more!

Here comes self adjusting timers to the rescue!

Tested on -

  • Nodejs
  • Browsers
  • React Native

How does it work

Javascript is a scripting language with runs on one process, due to that both setInterval and setTimeout are not accurate because the process can be busy with other stuff.

Setting a traditional interval will very quickly will prove itself inaccurate, in this picture you can see the offset which is produced by these functions

  setInterval(() => { /* Actually called late/early. */ }, 1000);

wrong

This library will calculate the delta between callbacks and will aim to hit your expectation every time, very much like a game stepping machanism (game time accumalation is anotehr library I wrote for that)
right

Also, javascript can be paused on slowed down to a hault (for example when your user exits your react native app, switching between tabs, or even by rendering UI), so instead of running your callback bunch of times for every missed second, your callback gets to run once with a ticks parameter which tells how much to "tick" by
batch

Usage

  • Install
      npm install --save self-adjusting-interval
      # or yarn
      yarn add self-adjusting-interval
  • import and use!

    import setSelfAdjustingInterval from 'self-adjusting-interval';
    
    setSelfAdjustingInterval(ticks => { // number, usually 1 
    // Your code...
    // for example, updating the UI clock with seconds
      addSecondsToClock(ticks);
    }, 1000); // ms