auto-interval

A newer, timing-error-free version of setInterval()

Usage no npm install needed!

<script type="module">
  import autoInterval from 'https://cdn.skypack.dev/auto-interval';
</script>

README

setAutoInterval

setAutoInterval (auto-interval) is a newer ECMAScript setInterval implementation which balances cumulative timing errors.

Example with setInterval

var intervalID = setInterval(() => console.log("The Date.now() is ", Date.now()), 100)
setTimeout(clearInterval, 5000, intervalID) // Finish after 5s

Example with setAutoInterval

const { setAutoInterval } = require("auto-interval")
var clearAutoInterval = setAutoInterval(() => console.log("The Date.now() is ", Date.now()), 100)
setTimeout(clearAutoInterval, 5000) // Finish after 5s

What's the difference?

Comparison of gap between ETA (Expected Time) and Actual Run Time in setInterval() and setAutoInterval().

Demo

Time Table for setInterval()

Demo

Time Table for setAutoInterval()

Demo

Advantages

  • Same function signature as setInterval()
  • Ultra-lightweight
  • Minimum code adaptation

Demos and Tools

Demonstration is here.

Demo

Demo

License

Copyright (C) 2020 Lautaro Capella (twitter: @PkuyApp).