intervaly

0.1.4 • Public • Published

Intervaly

A simple API that allows you to run functions on an interval using the more reliable setTimeout strategy. All functions are run as async in order to make sure that async methods follow the timeout.

NPM version Known Vulnerabilities npm NPM downloads issues license Gitter

Table of contents

Why

I created invervaly because I was sick of creating the same methods over and over again to use setTimeout for interval functionality instead of setInterval since it's more reliable. I also wanted to bind multiple tasks to the same interval timer instead of creating multiple ones.

Install

To install it as a global command to use anywhere you can use:

$ npm install intervaly

Usage

To use intervaly in your project in a Node environment use:

const Intervaly = require('intervaly');
 
const invervaly = new Intervaly();

If you're in a browser/webpack environment use:

import Intervaly from 'intervaly';
 
const intervaly = new Intervaly();

Basic Example

const Intervaly = require('intervaly');
 
const intervaly = new Intervaly();
 
/**
 * Say 'Hello'.
 * 
 * We want this task to run every 2 seconds.
 */
const hello = () => {
  console.log('Hello');
};
 
/**
 * Say 'World'.
 * 
 * We want this task to run every 3 seconds.
 */
const world = () => {
  console.log('World!');
};
 
intervaly.addTask('hello', hello, 2000).addTask('world', world, 3000);

Note the simple chainable API allowing you to easily create interval timers. While some implementations of similar programs don't have a name parameter I find that it makes removing and working with tasks much faster and wastes less resources.

Initialization

When you create a new instance of Intervaly, there are several options that can be provided:

param type description default
options Object
options.interval number The amount of time that should pass between ticks of the timer. 1000
options.autostart boolean Indicates whether or not Intervaly will start the timer as soon as it its initialized. false

API

start

Starts the interval timer. If the autostart option is set to true then this method will be called automatically after Intervaly is finished initializing.

Example

const hello = () => {
  return 'Hello World!';
};
 
intervaly.addTask('hello', hello);
 
intervaly.start();

stop

Stops the operation of the timer.

Example

const hello = () => {
  return 'Hello World!';
};
 
intervaly.addTask('hello', hello);
 
intervaly.start();
 
intervaly.stop();

addTask

Adds a task to be run on an interval. This returns the created task.

param type description default
name string The name of this task used to modify/remove it.
fn Function The function to run on an interval.
interval number The interval that this task should run at. 1000

Example

const hello = () => {
  return 'Hello World!';
};
 
intervaly.addTask('hello', hello);

removeTask

Removes a task from Intervaly by its name.

param type description default
name string The name of the task to remove.

Example

const hello = () => {
  return 'Hello World!';
};
 
intervaly.addTask('hello', hello);
 
intervaly.removeTask('hello');

clear

Removes all tasks from the timer.

const hello = () => {
  return 'Hello World!';
};
 
intervaly.addTask('hello', hello);
 
intervaly.clear();

Tests

To run the tests available for intervaly, use:

$ npm run test

License

MIT

Dependencies (0)

    Dev Dependencies (14)

    Package Sidebar

    Install

    npm i intervaly

    Weekly Downloads

    1

    Version

    0.1.4

    License

    MIT

    Unpacked Size

    72.7 kB

    Total Files

    19

    Last publish

    Collaborators

    • robert.corponoi