/intervalometer

Call a function at every frame or every X ms. With start/stop

Primary LanguageJavaScriptMIT LicenseMIT

intervalometer

Call a function at every frame or every X ms. With start/stop

gzipped size Travis build npm link

Usage

There are two times of intervalometers: frame-based and time-based. Both return an object with start and stop functions. You can safely call start multiple times, the callback will only run once at every interval.

frameIntervalometer(cb)

Uses requestAnimationFrame and therefore calls the provided callback at every frame. Ideal for animations

const painter = frameIntervalometer(function (millisecondsSinceLastFrame) {
	// your logic to run at every frame
});

button.onclick = function () {
	painter.start();
};

timerIntervalometer(cb, ms)

uses setTimeout and calls the callback every ms milliseconds

const poller = timerIntervalometer(pollingFunction, 100); //runs every 100 ms

startButton.onclick = function () {
	poller.start();
};
stopButton.onclick = function () {
	poller.stop();
};

Install

Pick your favorite:

<script src="dist/intervalometer.browser.js"></script>
<!-- use as intervalometer.frameIntervalometer(cb) -->
<!--     or intervalometer.timerIntervalometer(cb, ms) -->
npm install --save intervalometer
var i = require('intervalometer');
var frameIntervalometer = i.frameIntervalometer;
var timerIntervalometer = i.timerIntervalometer;
import {frameIntervalometer, timerIntervalometer} from 'intervalometer';

Dependencies

None! frameIntervalometer only works where window.requestAnimationFrame is available (yes in modern browsers; not in Node)

Related

License

MIT © Federico Brigante