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

@bfattori/neotimer-compat

v1.0.0

Published

A small libaray of timer abstractions to make using timers in Javascript easier.

Downloads

7

Readme

NeoTimer

A tiny JavaScript timer library

Originally extracted from a game engine I wrote, I found these timers extremely useful elsewhere. It provides five little timer abstractions which make using timers in Javascript a little easier. Everything uses setTimeout to function because setInterval is inherently unstable for repetition. Additionally, the global timer functions are not modified in any way. This is all self-contained.

A Timer is a simple JavaScript timeout with some added functionality. As long as the timer instance exists, it can be restarted and perform the action again. You can also change the interval when the timer is not running.

This version is written to be compatible with ES5 and earlier. For the version compatible with ES6 and later, try: https://npmjs.org/packages/timersjs

// Create the timer instance
let timer = NeoTimer.timer(1000, function() {
    alert("NeoTimer!");
});
// expect: "NeoTimer!" after one second

// Change the interval to 5000 and restart
timer.interval(5000).restart();
// expect: "NeoTimer!" after five seconds

// When done with a timer, kill it explicitly
timer.kill();

Installation

npm install timersjs

Usage:

Browser

<script src="path/to/timers.js" type="application/javascript"></script>

Node

const NeoTimer = require('neotimer');

A simple timer:

let timer = NeoTimer.timer(100, function(delta, now) 
   console.log(`This is output after 100ms, the current time is ${now}ms`);
);

Note: Don't use arrow functions, and instead use anonymous functions. Unexpected results can occur otherwise.

The function is given the timer as the context so within the function, this refers to the timer. A timer can be turned into an interval simply by calling the timer's restart() method:

let timer = NeoTimer.timer(250, function() {
   console.log("This is output every 250ms");
   this.restart();
});

Some syntactic sugar and you don't have to restart it yourself:

let repeater = NeoTimer.repeater(250, function(delta) {
   console.log(`This is output every 250ms, time since last execution: ${delta}ms`);
});

Timers are system objects that need to be maintained. When you are done with a timer, always clean it up. Within a timer, it is possible to achieve this by calling the kill() method of the timer.

NeoTimer.timer(50, function() {
   if (ajaxHasReturned()) {
      this.kill();
   } else {
      this.restart();
   }
});

This form of a timer will automatically kill itself when it completes so you don't have to remember to kill it:

NeoTimer.oneShot(500, function() {
   console.log("This is called when the timeout is complete");
});

This is a timer which will repeat a certain number of times before it self destructs:

// Repeat every 250 ms, 8 times in total (2 seconds minimum)
NeoTimer.multi(250, 8, function(repetition) {
   console.log(`This is repetition #${repetition}`);
}, function() {
   console.log("The multi timer is complete");
});

Finally, this form runs a repeater internally:

// Over a period of 5 seconds, run the repeater as often as possible with a minimum delay of half a second
NeoTimer.trigger(5000, function() {
    console.log("The trigger is complete");
}, 500, function(now, delta) {
    console.log(``Triggered @ ${delta}ms``);
});

You may wonder why have the trigger and the multi? Well, the multi is triggered a specific number of times with the same interval between callbacks. It is guaranteed to call back the number of times you want it. The trigger. on the other hand, will trigger as often as it can in the time allotted.

License

Copyright 2013, 2019 Brett Fattori

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following 
conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.