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

mb-timer

v0.1.2

Published

Precision JavaScript timer for recurring events.

Downloads

2

Readme

mb-timer

Precision JavaScript timer for recurring events

Description

Adds a global object mbTimer, which can be used in place of setTimeout and setInterval in need of more precise and self-correcting recurring timer events.

Usage

Simply load the script into your page (<script src='/path/to/mb-timer.min.js></script> or using Require or any other method).

Timers can be created by calling the constructor, which takes two parameters: the duration in milliseconds between executions, and the function to execute after each delay.

var mytimer = new mbTimer(100, function() {
    console.log('Do stuff here');
});

The timer will need to be started by calling mytimer.start() (This will return your timer instance)

The timer can be stopped by calling mytimer.stop() (This will return your timer instance)

The timer may also be paused for a duration, and will resume automatically when that time is over.

// Pause me for 2 seconds!
mytimer.pause(2000);

Debug mode can be enabled by passing true as the first parameter to the start function, eg: mytimer.start(true) will output metrics to the console.

Under-the-hood

Plain JS timers (setTimeout, setInterval) are relatively inaccurate. The browser takes the given duration as a 'recommendation', and will wait at least that long. Meaning that the following may happen 500, 504, or 2043 milliseconds after it is called.

setTimeout(function() {
    console.log('What day is it?!')
}, 500);

mbTimer works by registering its own setTimeout calls, and giving it a wrapped version of the user-submitted execution function. Each time the wrapper function is called, it will measure the elapsed time using the Date object, and register a new setTimeout with a reduced / increased duration to counteract any inaccuracies in the setTimeout function.

Three metrics are provided in debug mode: delta, offset, and drift.

  • delta is the real time elapsed between executions (using the Date object)
  • offset is the difference between the delta and the timer's registered delay
  • drift is the accumulated difference between the current time, and the expected time after the current number of executions

Ideally, both offset and drift will equal 0. Due to the nature of things, that will rarely be the case. Much more often, they will both be less than 5 milliseconds. That is still far, far better than setInterval can accomplish.

Compatibility

The plugin has not yet been rigorously tested, but should work in all modern browsers, given the browser window has focus.

NOTICE: Some browsers will reduce the importance of timer events if the browser window loses focus! This means that if a user clicks out of your browser window, events may not happen at the correct time until the window is focused again. At that time, the mbTimer will work to catch up for lost time.

Contact & License Info

Author: Matthew Balmer
Twitter: @mattbalmer
Website: http://mattbalmer.com
License: MIT