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

safe-timers

v1.1.0

Published

Timers with near-infinite duration support

Downloads

197,058

Readme

Safe Timers

About

Q: What's this all about? Aren't JavaScript timers safe? A: Long story short: they're a bit broken. This module unbreaks them.

Whether it's by spec, or by accident, all major browsers and Node.js limit the interval a setTimeout can accept to a 32 bit signed integer. What that means in essence is that a timeout can never last longer than 24.85 days. Long enough, right?

The problem is that:

  • In human (non-binary) terms, this is a really arbitrary number.
  • In long running processes (whether on the web, or in Node), you are limited.
  • If the interval you provide overflows this limit, the timer fires immediately!

All the arguments about "you shouldn't need intervals this big anyway" go out the window the moment you provide a big one and instead of never firing, it fires immediately. This is a real problem. And so here we are, Safe Timers solves this for you.

Does that mean you should forego the browser native setTimeout and setInterval altogether? Absolutely not. Most of the time, we pass constant short intervals, in which case Safe Timers are overkill. But when your interval comes from some variable that depends on state or user input, using Safe Timers is a good idea.

API

Timer setTimeout(Function callback, number interval, mixed arg1, mixed arg2, ...)

Calls callback after at least interval milliseconds have passed. All arguments passed after the interval will be passed to the callback once it gets invoked. Returns a Timer instance.

const setTimeout = require('safe-timers').setTimeout;

setTimeout(function (msg) {
  console.log(msg);
}, 5000, 'Hello world');

Timer setTimeoutAt(Function callback, number timestamp, mixed arg1, mixed arg2, ...)

Calls callback when our clock reaches the given timestamp (in milliseconds). All arguments passed after the interval will be passed to the callback once it gets invoked. Returns a Timer instance.

const setTimeoutAt = require('safe-timers').setTimeoutAt;

setTimeoutAt(function (msg) {
  console.log(msg);
}, Date.now() + 5000, 'Hello world');

Interval setInterval(Function callback, number interval, mixed arg1, mixed arg2, ...)

Calls callback after at least every interval milliseconds. All arguments passed after the interval will be passed to the callback when it gets invoked. Returns an Interval instance.

const setInterval = require('safe-timers').setInterval;

setInterval(function (msg) {
  console.log(msg);
}, 5000, 'Hello world');

timer.clear() / interval.clear()

The response from safetimers.setTimeout[At] and safetimers.setInterval are Timer and Interval objects respectively. To cancel a timer or interval, you can call clear on it.

const setTimeout = require('safe-timers').setTimeout;

const timer = setTimeout(function (msg) {
  console.log(msg); // this will never show
}, 5000, 'Hello world');

timer.clear();