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

dynamic-interval

v1.2.1

Published

The dynamic setInterval

Downloads

93

Readme

dynamic-interval

:clock1: The dynamic setInterval


build npm

setInterval with the ability to specify a new interval duration on each tick.

Also referred to as a "dynterval".

Sections

Install

npm install dynamic-interval

Usage

import setDynterval from 'dynamic-interval'

const dynterval = setDynterval(ctx => console.log('tick!', ctx), 100)

Examples

Basic

This script doubles the duration of the interval on each iteration, starting with 50ms:

import setDynterval from 'dynamic-interval'

// you can attach arbitrary properties to this object (in this case, `rate`), but
// `wait` is what's used to determine the duration between each interval
const config = { wait: 50, rate: 2 }

const dynterval = setDynterval(context => {
  console.log('interval', context)

  const next = context.wait * context.rate

  return { ...context, wait: next }
}, config)

// interval { wait: 50,  rate: 2 }
// interval { wait: 100, rate: 2 }
// interval { wait: 200, rate: 2 }
// ...

// clear out the interval after 2 seconds
// NOTE: `window.clearInterval` is not compatible! use the `clear` method instead
setTimeout(() => {
  dynterval.clear()
}, 2000)

Advanced

This script calculates the amount of drift on each step and corrects for it during the subsequent step.

It uses a custom interval api. In this case, we're using worker-timers.

import setDynterval from 'dynamic-interval'
import * as workerTimers from 'worker-timers'

const setAccurateInterval = (func, wait) => {
  let expected = Date.now() + wait

  return setDynterval(context => {
    const drift = Date.now() - expected

    if (drift > wait)
      throw Error(`that drift be crazy: ${drift}`)

    expected += wait

    const next = Math.max(0, wait - drift)

    func(context)

    return { ...context, drift, wait: next }
  }, wait, workerTimers)
}

setAccurateInterval(context => console.log('tick', context), 1000)

Interface

setDynterval(<action>, <wait|config>, <api>)

action

The callback to invoke on each interval tick

  • Type: Function
  • Required

wait

Specifies the duration of each interval (i.e. the amount of time to wait between each tick)

  • Type: Number

config

Specifies the configuration of the interval. Passed into the action function as context.

  • Type: Object

  • Properties:

    • wait

      Specifies the duration of each interval

      • Type: Number
    • immediate

      Determines if the interval should start immediately or wait one interval before starting

      • Type: Boolean
      • Default: false

api

A custom interval api may be provided. It must define functions for both setInterval and clearInterval.

Related

  • stateful-dynamic-interval adds pause, resume and grouping functionality to dynamic-interval.
  • accurate-interval an interval that automatically corrects for local drift on each tick. May be provided as an api.
  • audio-context-timers an interval that uses the Web Audio API clock. May be provided as an api.
  • worker-timers an interval that uses Service Workers as a backend. May be provided as an api.

License

MIT