Socket
Book a DemoInstallSign in
Socket

mindelay

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mindelay

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

1.0.7
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
2
Weekly downloads
 
Created
Source

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.

Keywords

delay

FAQs

Package last updated on 09 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.