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

@jasonpollman/dynamic-interval

v1.0.0

Published

Like `setInterval`, but the delay is a function.

Downloads

2

Readme

@jasonpollman/dynamic-interval

For the rare cases when you might need an interval where the delay between calls is dynamic.
This small utility library exports two functions setDynamicInterval and clearDynamicInterval.

Install

Via NPM:

npm install @jasonpollman/dynamic-interval --save

For the browser:
dist/dynamicInterval.min.js is UMD

<script src="dist/dynamicInterval.min.js"></script>
<script>
  var setDynamicInterval = dynamicInterval.setDynamicInterval;
  var clearDynamicInterval = dynamicInterval.clearDynamicInterval;
</script>

Usage

import { setDynamicInterval } from '@jasonpollman/dynamic-interval';

const baseTimes = [1000, 2000, 3000];

function invervalScheduler(baseTimes) {
  return baseTimes.shift() || 1000;
}

const timerReference = setDynamicInterval(() => console.log('Hello World!'), intervalScheduler));
// Logs "Hello World!" after 1 second, then 2, then 3, then every 1 second.

API

setDynamicInterval({function} callback, {function|Array|number} intervalScheduler, {...any} params) => {object}

Calls callback every intervalScheduler() milliseconds and returns a reference for clearing.

setDynamicInterval takes in two functions: callback and intervalScheduler. All remaining arguments are passed to callback at every invocation.

  • callback is called for each tick of the interval.
  • intervalScheduler is a method that should return a numeric value (the next delay of the interval).

Note, if intervalScheduler returns a non-numeric or negative value the interval will terminate.
While debatable, this is by design and differs from the behavior of setInterval (which in most cases will use 0 or some other "immediate" value for NaN).

intervalScheduler also supports Array types, which will iterate over each value in the array and terminate when either a non-numeric value is found or all values have been exhausted.

clearDynamicInterval({object} dynamicIntervalReference) => {undefined}

Clears a dynamic interval reference. Softly fails for all other misuse.

Examples

Double Time

Sets an interval that's twice as long as the previous, starting with one second.

let multiplier = 500;

function callback() {
  // Do something...
}

function intervalScheduler() {
  multiplier *= 2;
  return multiplier;
}

setDynamicInterval(callback, intervalScheduler);

Array Based

You can use an array—it will clear the interval once all values have been used once.
Note, the input array is left unmutated.

The following will run 3 times, first after 100ms, then 200ms, then 300ms.
The interval will then be cleared since no items remain in the array.

setDynamicInterval(() => { /* Do something... */ }, [100, 200, 300]);