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

multifeature-debouncer

v1.0.0

Published

Multi-feature javascript debouncer.

Downloads

3

Readme

Multi-feature Debouncer (npm package name)

Javascript debouncer that allows for debouncing on the leading, trailing, and leading and trailing edges and also allows for resetting the debounce interval. Resetting the interval allows for indefinitely postponing an operation until no calls have been made to the debounced function within the interval (e.g., the interval is 200ms and a call comes in at 80ms and another 80ms after that, resulting in the function not being called for 360ms (80ms + 80ms + 200ms)).

Example usage

npm install multifeature-debouncer

import {debounce} from "multifeature-debouncer";
// or
const debounce = require('multifeature-debouncer').debounce

function onScroll() { 
  // do some expensive operation on scrolling
}

const debouncedOnScroll = debounce(onScroll, {intervalMillis: 50});
// will be called on the trailing edge every 50ms
window.registerEventHandler('scroll', debouncedOnScroll);

Options

| name | required | data type | description | | ---- | -------- | --------- | ----------- | | intervalMillis | true | number | how long to wait before calling the function (is the interval that is reset) | | leading | false | boolean | whether or not to call the function on the leading edge of the debounce interval. defaults to false. | | trailing | false | boolean | whether or not to call the function on the trailing edge of the debounce interval. defaults to true*. | | resetTimeout | false | boolean | whether or not to reset the interval when an event occurs before the interval has expired. |

* Though trailing defaults to true if leading is specified and trailing is not then it will be considered false. See matrix below.

A matrix is the easiest way to ingest the leading-trailing edge options:

| Leading | Trailing | Edge used | | :---------- | :------------- | :-------: | | undefined | undefined | trailing | undefined | false | leading | undefined | true | trailing | false | undefined | trailing | false | false | throws error | false | true | trailing | true | undefined | leading | true | false | leading | true | true | leading and trailing

Reset example

// on scroll will NOT be called until the user has stopped scrolling for half a second (500ms)
debounce(onScroll, {intervalMillis: 500, resetTimeout: true})
/*
Alternatively, onScroll could be called on the leading edge when the user scrolls and not again 
until the user stops scrolling for 500ms and then resumes again.
*/
debounce(onScroll, {intervalMillis: 500, resetTimeout: true, leading: true})

// MORE CONCRETE EXAMPLE:
function delay(time) {
  return new Promise(resolve => {
    setTimeout(() => {

    }, time)
  })
}

function logI(i) {
  console.log(i);
}

const debouncedI = debounce(logI, {intervalMillis: 100, resetTimeout: true})

debouncedI(0)  // not logged
delay(50).then(() => {
  debouncedI(1)  // not logged, because next delay is not 100ms
  return delay(50);
}).then(() => {
  debouncedI(2) // not logged, because next delay is not 100ms
  return delay(50);
}).then(() => {
  // still nothing has been logged, even though 150ms have elapsed. 
  debouncedI(3) // logged, because debouncedI is not called again for 100ms
  // be careful with relying on debouncing right at the edge of the interval timeout..
  return delay(100);  
}).then(() => {
  debouncedI(4);
  return delay(100);
})
// only '3' and '4' area logged from the above chain

Leading/Immediate

// called immediately when the user scrolls and not again for 500ms.
debounce(onScroll, {intervalMillis: 500, leading: true});