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

mindelay

v1.0.7

Published

Sets a minimum delay before a callback can be called, no matter how long it takes for the caller to call it.

Downloads

15

Readme

mindelay CircleCI Status NPM Version NPM Downloads bitHound Overall Score

Pronounced as one word. Sets a minimum delay before a callback can be called, no matter how long it takes for the caller to call it.

Spec

function mindelay(function callback, number delayMS)
  // Adds a minimum delay in milliseconds to a callback, however long the caller takes to call it.
  // Returns callback wrapped with delay code.
  //   If the wrapped callback is called before the delay is expired, it still waits until the end of the delay.
  //   If the wrapped callback is called after the delay is expired, it executes immediately.
  //   wrappedCallback.call always contains the original callback, if you need to call it directly with no delay.
  //   Alternatively, calling wrappedCallback.cancel() will cancel any delay, so that next time you call the wrapped callback it will execute immediately.
  //
  // If arguments callback, delayMS are reversed, it'll still work fine.
  // An exception is thrown if arguments have incorrect types.
  //

Install

On command line:

$ npm install --save mindelay

In NodeJS:

let mindelay = require('mindelay');

Alternatively, in browser JavaScript:

<script src="path/to/mindelay.js"></script>

Usage

We're using the usecase of an API response, which illustrates the utility of mindelay best and is how we use mindelay in production.

Instead of something like this, which responds as soon as the API responds

apiCall(data, function(response){
  //blah
});

or something like this, which adds a fixed delay of one second to the API response time, no matter how long the API response time

apiCall(data, function(response){
  setTimeout(function(){
    //blah
  }, 1000)
});

use something like this, which delays by at least one second, but if the API takes a long while it will respond as soon as possible.

apiCall(data, mindelay(function(response){
  //blah
}, 1000));

Cancellation

var wrappedCallback = mindelay(function(response){
  //blah
}, 1000));

If you ever need to reference the callback directly, use wrappedCallback.call:

wrappedCallback.call(/*...*/) //will have no delay

If you want to cancel any delay on a wrapped callback, use wrappedCallback.cancel:

wrappedCallback.cancel()
wrappedCallback() //will have no delay

Example

Example: chatbot

Pictured is a chatbot (SkillFlow) that needs to make a query to a Natural Language Processing API before it's able to respond. We want to add a somewhat natural delay, but must keep in mind that the API may take any amount of time to respond, and we can't just use setTimeout and keep the user waiting for extra long.

The solution is of course to use mindelay instead of setTimeout.


Copyright ©2016 SkillFlow. MIT License. Created by Clive Chan, with contributions from David Tesler.