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

timer

v1.0.1

Published

timeout utilities (promisfied setTimeout, chained timeouts)

Downloads

822

Readme

this package provides a utility function to promisify and unify setTimeout and setInterval functions.

It also provides a comfortable way to chain multiple setTimeouts with different timeouts and even a finally setInterval with the same callback.

An additional feature is the auto interval function which applies a simple setInterval which triggers at event time slots, ie if you call the timer.auto function with an interval of 15601000 (15 minutes) your callback will be triggered at 00:15:00, 00:30:00, and so on.

USAGE EXAMPLE (JAVASCRIPT):

// as a promise in order to delay execution
await timer(300);

// use timer with Promise.race to define timeouts:
// timer.timeout will reject the promise after the given timeout
Promise.race( [otherPromise, timer.timeout(400)] );


// simple setTimeout
// same as setTimeout( cb, 200);
timer(200, cb);


// setTimeout with same callback after 200ms and after 400ms
// the second array element is also 200ms because it is set up after the first one (200ms+200ms = 400ms)
timer( [200,200], cb );
// same as 
// setTimeout( 200, cb );
// setTimeout( 400, cb );

// simple setInterval
timer.interval(2000,cb);
// same as setInterval(cb,2000);

// setting up a interval after a timeout
timer(500,2000,cb);
// this one will trigger cb after:
//  500ms, 2500ms, 4500ms, 6500ms, ...


// auto slot a interval (for example if you want your callback to be triggered at 00:15:00, 00:30:00, ..)
timer.auto(15*60*1000, cb);




// clearing a timeout:
o = timer(500,cb);
o.clear();

// if you use the clear() function on a timer with multiple timeouts or intevals, ALL of them will be cleared,
// so your callback won't be triggered any more

USAGE EXAMPLE (COFFEESCRIPT):

# with coffeescript the utility functions are even more handy

timer 200, -> console.log 'hello'

timer [500,200], -> console.log 'hello after 500ms and 700ms'

# BONG every 30min at even time slots (0:00, 0:30, ..)
{clear} = timer.auto 30*60*1000, -> console.log 'BONG'
# end the BONG interval after 5h
timer 5*60*60*1000, clear

QUESTIONS or feedback? you are welcome, send me an email at [email protected]