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

generator-ticker

v1.1.1

Published

An ES6 generator similar to setInterval that adjusts for slow receivers.

Downloads

78

Readme

generator-ticker

NPM Package GitHub Repo Code Style: Google

What is this?

Do things, particularly async things, on a regular interval. Avoid drift, by treating the interval as a maximum rate, adjusting for slow receivers, and dropping missed ticks if necessary.

This implementation was inspired by Go's time.NewTicker.

Usage

const {newTicker} = require('generator-ticker');

// the last three parameters (count, initial, start) are optional
for await (const tick of newTicker(500, 3, false, new Date(0))) {
  console.log(`${tick.getSeconds()}s${tick.getMilliseconds()}ms\n`);
}

// will output something similar to:
// 52s500ms
// 53s0ms
// 53s500ms

Installation

Install or import the NPM package generator-ticker. Should work in most environments, including browsers, Node.js, and Deno.

API

Table of Contents

newTicker

Creates an asynchronous ticker that yields Date objects at a specified interval. The ticker will adjust the interval or drop ticks, to make up for slow receivers. It avoids drift by using the time from the last tick as the basis for the next tick.

Parameters

  • interval number {number} The interval duration, in milliseconds. Must be a number >= 0.
  • count {number} The number of times to tick. A negative value indicates an unlimited number of times. Defaults to -1 (unlimited). (optional, default -1)
  • initial {boolean} If true, the ticker will immediately yield the current Date as the first tick. Defaults to false. (optional, default false)
  • start any? {Date} The start time for the ticker. Defaults to the current time. This is useful if you want to lock your ticker to certain wall times (requires picking an interval that divides evenly into 24 hours). This parameter interacts with the initial parameter. If initial is true, the first tick will be at the start time. Otherwise, the first tick will be at the start time + interval.

Returns AsyncGenerator<Date, void> An AsyncGenerator that yields Dates at the given interval.