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 🙏

© 2026 – Pkg Stats / Ryan Hefner

recursive-timeout

v0.0.10

Published

A simple solution to a classic problem: setInterval implemented as recursive setTimeout calls.

Readme

recursive-timeout

npm version License: MIT

A simple solution to a classic problem: setInterval implemented as recursive setTimeout calls.

This package is designed to be a drop-in replacement for the normal setInterval.

"Why exactly do I need this?"

Take a look at this section from javascript.info, it outlines the problem and the solution.

Basically, the normal setInterval doesn't wait for the callback to happen, – it schedules the next iteration right away:

function runsForOneSecond() {
  const startTime = Date.now()

  while (Date.now() - startTime < 1000) { /* just wait */ }

  console.log(Date.now(), 'Done!')
}

console.log(Date.now(), 'Start')
setInterval(runsForOneSecond, 500)
// logs (approximately):
// 1234567890000 Start
// 1234567891500 Done!
// 1234567892500 Done!
// 1234567893500 Done!

… while the "recursive timeout" approach necessarily waits for the callback to finish before scheduling the next call:

import { setRecursive } from 'recursive-timeout'

console.log(Date.now(), 'Start')
setRecursive(runsForOneSecond, 500)
// logs (approximately):
// 1234567890000 Start
// 1234567891500 Done!
// 1234567893000 Done!
// 1234567894500 Done!

Features

  • Dual Module Support: Works seamlessly with both ECMAScript Modules (import) and CommonJS (require).
  • Familiar API: Designed as a drop-in replacement for setInterval.
  • Promise-based API: ⚠️ (coming soon) ⚠️ A promise-based interface for use with asynchronous callbacks.

Installation

npm install recursive-timeout

Usage

The straightforward API of setRecursive and clearRecursive mirrors the native setInterval and clearInterval functions.

ECMAScript

This is the standard way to use the package, ideal for most cases.

import { setRecursive, clearRecursive } from 'recursive-timeout'

setRecursive(() => console.log('hi'), 1000)
setRecursive(console.log, 1000, 'hi')

To cancel the interval, pass the timer into clearRecursive or the standard clearInterval, or even clearTimeout:

const recursive = setRecursive(console.log, 1000, 'hi')

clearRecursive(recursive)
clearInterval(recursive) // ✅ works
clearTimeout(recursive) // ✅ also works

For TypeScript users, some additional checks are added:

function sum(a: number, b: number): void {
  console.log(a + b)
}

setRecursive(sum, 100)
// ❌ Error: not enough arguments

setRecursive(sum, 100, 42)
// ❌ Error: not enough arguments

setRecursive(sum, 100, 42, 17)
// ✅ OK (logs 59 every ~100 milliseconds)

ECMAScript (promise-based) – coming soon ⚠️

import { setRecursive, clearRecursive } from 'recursive-timeout/promises'
import { promises } from 'recursive-timeout'

promises.setRecursive(…)
promises.clearRecursive(…)

CommonJS

const { setRecursive, clearRecursive } = require('recursive-timeout')

CommonJS (promise-based) – coming soon ⚠️

const { setRecursive, clearRecursive } = require('recursive-timeout/promises')
const { promises } = require('recursive-timeout')

promises.setRecursive(…)
promises.clearRecursive(…)

License

This project is licensed under the MIT License.