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

rebounce

v2.0.0

Published

Usefull for implementing behavior that should happen less frequently than default

Downloads

5

Readme

rebounce

Usefull for implementing behavior that should happen less frequently than default.

Example

Node

var rebounce = require('rebounce');

// want 
var rebounced = rebounce(original, 100)

// call `rebounced` every 50ms

function original() {
}
Time  Rebounced Original
----------------------------------------
0     called    called
56    called    -
105   called    called
157   called    -
211   called    called
266   called    -
317   called    called
372   called    -
423   called    called
476   called    -

browser

<script src="dist/rebounce.js" type="text/javascript"></script>
<script type="text/javascript">
  window.onresize = rebounce(resize, 1000, true);
  var count = 0;
  function resize(e) {
    console.log(count++, '*'.repeat(80));
    console.log('height', window.innerHeight);
    console.log('width', window.innerWidth);
  }
</script>

API

rebounce(fn, interval, [ once = false ])

Creates and returns a rebounced version of the passed function that will execute the original fn when either fn has not been called yet, or interval miliseconds have elapsed since the last execution of fn.

However, if once is set to true, fn will be called when either fn has not been called yet, or interval miliseconds have elapsed since the last execution of the rebounced version of fn.

Suppose that the rebounced version will be scheduled to execute at intervals [50, 100, 50, 50, 100, 50, 50, 50], and interval is set to 100, once to false, then the actual times of execution of fn and its rebounced version will be like:

Time  Rebounced Original
----------------------------------------
0     1         1
55    2         -
161   3         2
212   4         -
263   5         3
366   6         4
422   7         -
471   8         5
527   9         -

If once is set to true,

Time  Rebounced Original
----------------------------------------
0     1         1
53    2         -
156   3         -
157   -         2
210   4         -
266   5         -
370   6         3
422   7         -
474   8         -
529   9         -

risingTrigger v.s. fallingTrigger

By default, the rebounced version executes fn at the beginning of the interval. But you can use rebounce.fallingTrigger to execute fn at the end.

var risingTrigger = require('rebounce');
var fallingTrigger = require('rebounce').fallingTrigger;

In the above example,

when using risingTrigger:

Time  Rebounced Original
----------------------------------------
0     1         1
55    2         -
161   3         2
212   4         -
263   5         3
366   6         4
422   7         -
471   8         5
527   9         -

when using fallingTrigger

Time  Rebounced Original
----------------------------------------
0     1         -
57    2         -
106   -         1
160   3         -
216   4         -
265   -         2
267   5         -
371   -         3
372   6         -
424   7         -
472   -         4
475   8         -
531   9         -
581   -         5