npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

intervaly

v0.1.4

Published

Run any function on an interval using the more reliable setTimeout strategy.

Downloads

3

Readme

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 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