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

interval-events

v1.0.3

Published

A simple way to execute events at specific time intervals

Downloads

3

Readme

IntervalEvents

IntervalEvents is an NPM module that provides a simple way to execute events at specific time intervals. It allows you to set up actions to be executed at predetermined moments and control the start, pause, resume, and stop of the event sequence.

Installation

To use IntervalEvents, you need to install the module using npm:

npm install interval-events

Usage

First, import the IntervalEvents module into your project:

const IntervalEvent = require("interval-events");

Next, create an instance of IntervalEvent:

const event = new IntervalEvent();

Adding actions

You can add actions to the event sequence using the addAction(seconds, callback) method. The seconds parameter indicates the number of seconds to wait before executing the action, and callback is a function that will be called at that time.

Here's an example of how to add some actions:

event.addAction(5, () => {
  console.log("Action in 5 seconds");
});

event.addAction(5, () => {
  console.log("Action in 10 seconds");
});

event.addAction(10, () => {
  console.log("Action in 20 seconds");
});

In this example, three actions have been added. The first one will be executed after 5 seconds from the start, the second one after 10 seconds (5 seconds after the first action), and the third one after 20 seconds from the start (10 seconds after the second action).

Starting and stopping the event sequence

To start the event sequence, use the start(startAt) method:

event.start();

This will initiate the execution of the actions at the specified time intervals, if you indicate a number in startAt, the sequence of events will start from that second, otherwise it will start from the beginning.

If you want to stop the event sequence at any time, you can call the stop() method:

event.stop();

This will stop the execution of the actions and reset the event index to zero.

Pausing and resuming the event sequence

If you need to temporarily pause the event sequence, you can call the pause() method. This will stop the execution of the actions but maintain the current index, which means that when the sequence is resumed, it will continue from where it left off.

event.pause();

To resume the event sequence after pausing, use the resume() method. This will initiate the execution of the actions from the point where they were stopped.

event.resume();

Checking if the sequence is running

If you need to check if the event sequence is running, you can call the isRunning() method. This method returns true if the sequence is running and false otherwise.

const running = event.isRunning();
console.log("The sequence is running:", running);

Getting Information about the events

If you want to retrieve information about the scheduled events, you can call the getEvents() method. This will return an array with all the configured events, including the interval and the associated callback function.

const events = event.getEvents();
console.log("Configured events:", events);

Example

Here's a complete example of how to use IntervalEvents:

const IntervalEvent = require("interval-events");

const event = new IntervalEvent();

event.addAction(5, () => {
  console.log("Action in 5 seconds");
});

event.addAction(5, () => {
  console.log("Action in 10 seconds");
});

event.addAction(10, () => {
  console.log("Action in 20 seconds");
});

event.start();

In this example, an instance of IntervalEvent is created and three actions are added. Then, the event sequence is started, and the corresponding messages will be printed at the specified time intervals.

License

This project is licensed under the MIT License. See the LICENSE.