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

rifraf

v2.0.3

Published

A simple requestAnimationFrame wrapper with added iteratee creator.

Downloads

5,055

Readme

rifraf

A simple requestAnimationFrame (rAF) wrapper/polyfill with added iteratee creator.

Polyfill notes

  • The polyfill is a very naïve setTimeout wrapper. For a more robust polyfill, I recommend the raf module.
  • The default "frame-rate" for the polyfill is 120Hz or an 8ms delay.

API

All examples below assume that you've already required rifraf: var rifraf = require('rifraf');

The Basics

request(<Function> fn, <Object:optional> ctx)

Queues a callback to run before the next frame. Returns the rAF (or timeout, if polyfilled) handle. Pre-binds optional context object, if provided.

// rifraf.request returns the runtime-assigned handle that can be used to cancel the callback
var handle = rifraf.request(function () {
   // code to run before next frame 
});

cancel(handle)

Cancels a previously request using the returned handle.

// where handle is the return value of a rifraf.request call
rifraf.cancel(handle);

The Extras

iteratee(<Function> fn, <Object:optional> ctx)

alias: deferred

Used to defer expensive iterations or event handlers that need to wait until after all current DOM operations complete. Returns a new function that when called queues fn bound with ctx or its own this and its arguments.

// with context object
$('a[href]').each(rifraf.iteratee(function (i, el) {
    if ($(el).data('id') === this.id) {
        // expensive DOM ops here
    }
}, {id: 1}));

// without context object
$('a[href]').each(rifraf.iteratee(function () {
    var $el = $(this);
    // expensive DOM ops here
}));

delay(<Function> fn, <Object:optional> ctx, <Number:optional> _delay)

When you want to defer a function call, but your desired frame rate differs from native, delay is for you. Pre-binds context, if provided.

// with context
rifraf.delay(function () {
    console.log('My name is %s', this.name);
}, {name: 'Foo'});

// the next two are equivalent and will set the delay to ~24ms
rifraf.delay(function () {}, 24);

rifraf.delay(function () {}, null, 24);

delayed(<Function> fn, <Object:optional> ctx, <Number:optional> delay)

Used like iteratee, but when you want to delay not simply defer to next native frame. Call signature matches delay.

var delayedDefault = rifraf.delayed(function (i, el) {
    console.log('href:', this.href);
});

$('a[href]').each(delayedDefault);

var delayed24ms = rifraf.delayed(function () {}, 24);

sync120Hz()

Sets default delay time for delay, delayed (and polyfilled request and iteratee) methods to 8ms (roughly: 1000 / 120).

sync60Hz()

Sets default delay time for delay, delayed (and polyfilled request and iteratee) methods to 16ms (roughly: 1000 / 60).

sync30Hz()

Sets default delay time for delay, delayed (and polyfilled request and iteratee) methods to 33ms (roughly: 1000 / 30).

sync(<Number> delay)

Sets default delay time for delay, delayed (and polyfilled request and iteratee) methods to {delay}ms.