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

v1.0.2

Published

Truly pauseable, time-extendable, functionally dynamic intervals and timeouts in a lightweight package

Downloads

23

Readme

Interval Plus

Truly pauseable, time-extendable, functionally dynamic intervals and timeouts in a lightweight package

CI-CD

Features

  1. Pause and Resume
  2. Variable time interval
  3. Immediate function invocation
  4. Function updating
  5. Interval progress and status tracking
  6. Invocation skipping
  7. Asynchronous execution (async/await)

Motivation

JavaScript's setInterval and setTimeout offerings are fundamentally weak due to their inflexibility. To achieve their desired interval behavior, developers often have to re-instantiate intervals, manage or track interval instances, or implement other workaround solutions. I first developed this package to support the features above necessary in my personal projects, then decided to open source the package.

Getting Started

Install and Import

Node.js

npm i interval-plus
const { IntervalPlus, TimeoutPlus } = require("interval-plus")

// or

import {IntervalPlus, TimeoutPlus} from "interval-plus"

Browser

<script src="https://cdn.jsdelivr.net/npm/interval-plus@latest/src/index.js"></script>

Usage

Instantiation

const interval = new IntervalPlus(func, ms, options)
const timeout = new TimeoutPlus(func, ms, options)

Constructor Parameters

  1. func: function, required

    • a function to be invoked. Pass an async function to ensure asynchronous execution
  2. ms: number, required

    • how many milliseconds to observe before re-invoking func
  3. options: Object (optional)

    • name: String (optional), default "IntervalPlus"
      • An identifier to represent the IntervalPlus or TimeoutPlus object logs.
    • verbose: boolean (optional), default false
      • Whether or not to output logs to the console during key interval events
    • immediate: boolean (optional), default `false
      • Whether or not to immediately invoke func once instantiated

Instantiation Examples

const interval = new IntervalPlus(() => {
    console.log("it's been 1 second!")
}), 1000)

const timeout = new TimeoutPlus(() => {
    console.log("Execute once after 2 seconds")
}), 2000, {
    name: "myFirstTimeout",
    verbose: true
})

const immediateInterval = new IntervalPlus(() => {
    console.log("Execute now, then every 3 seconds")
}), 3000, {
    immediate: true
})

Operations

pause()

await interval.pause()

Allow the interval to sleep, freezing the amount of active time to elapse before next invocation.

This operation is idempotent - calling it multiple times sequentially is a the same as a single call.

If called during invocation, this operation will apply as soon as invocation terminates.


resume()

interval.resume()

Wake the interval from its sleep, counting down the remaining active time until next invocation.

This operation is idempotent - calling it multiple times sequentially is a the same as a single call.


stop()

await interval.stop()

Terminate the interval and its future invocation schedule.

Under the hood, this method calls clearInterval to ensure no ongoing asynchronous processes remain.

This operation is idempotent - calling it multiple times sequentially is a the same as a single call.

If called during invocation, this operation will apply as soon as invocation terminates.


changeInterval(ms)

await interval.changeInterval(2500)

Function Parameters

  1. ms: number, required

Adjust the interval's millisecond wait between invocations to equal ms.

If called during invocation, this operation will apply as soon as invocation terminates.


nextInvocationTime()

const nextDateObj = interval.nextInvocationTime()

Obtain a Date object corresponding to the next invocation, assuming no pausing.

Returns "now" if currently executing. Returns "paused" if paused.


nextInvocationActiveMs()

const nextMs = interval.nextInvocationActiveMs()

Get the number of active milliseconds until next invocation. Active means time passing while in a non-paused state.

Returns "now" if currently executing. Returns "paused" if paused.


prevInvocationStartTime()

const prevStart = interval.prevInvocationStartTime()

Obtain a Date object corresponding to the start of the previous invocation.

Returns undefined if no previous invocation.


prevInvocationEndTime()

const prevEnd = interval.prevInvocationEndTime()

Obtain a Date object corresponding to the end of the previous invocation.

Returns undefined if no previous invocation end.


prevInvocationStartActiveMs()

const prevStartMs = interval.prevInvocationStartActiveMs()

Get the number of active milliseconds since the previous invocation start. Active means time passed while in a non-paused state.

Returns undefined if no previous invocation.


prevInvocationEndActiveMs()

const prevEndMs = interval.prevInvocationeEndActiveMs()

Get the number of active milliseconds since the previous invocation end. Active means time passed while in a non-paused state.

Returns undefined if no previous invocation end.


skipToNextInvocation()

await interval.skipToNextInvocation()

Immediately skip forward to the next invocation, respecting a constant time interval afterwards.

If called during invocation, this operation will apply as soon as invocation terminates.


hasFutureInvocations()

const isAlive = interval.hasFutureInvocations()

Returns a boolean describing whether or not future invocations will be made. For instance, this function returns false once stop() has been called.