New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

delay-me

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

delay-me

A delayed function runs some code once a period of time has passed if the delayed function is not being called again in that time.

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

delay-me

npmnpmnpm

A delayed function runs some code once a period of time has passed if the delayed function is not being called again in that time.

Install

$ npm install --save delay-me

Main functionality

TypeScript

Delaying a function without recreating it (recommended way)

import {createDelayed} from 'delay-me';

const throttled_resize_handler = createDelayed(() => {
    console.log(`I'm a delayed callback. I'm doing some heavy calculations when window size changes`);
});

window.addEventListener('resize', () => throttled_resize_handler.set(250));

// You can force the callback to be executed immediately
// It will cancel the previously set timeout
throttled_resize_handler.now();

// Also, you can cancel a delay
throttled_resize_handler.cancel();

Delaying tagged function in a global delaying context

With this approach, if you will use the same tag for different situations - it will lead to unexpected behavior

import {delay} from 'delay-me';

window.addEventListener('resize', () => {
    const cancel = delay('throttled_resize_handler', 250, () => {
        console.log(`I'm a delayed callback. I'm doing some heavy calculations when window size changes`);
    });
    
    // You can cancel delayed function execution
    if(...){
        cancel();
    }
});

Delaying tagged function in specific context

With this approach, you can use the same tags in different contexts.

import {createDelayContext} from 'delay-me';

const delay1 = createDelayContext();

window.addEventListener('resize', () => {
    const cancel = delay1('throttled_resize_handler', 250, () => {
        console.log(`I'm a delayed callback. I'm doing some heavy calculations when window size changes`);
    });

    // You can cancel delayed function execution
    if(...){
        cancel();
    }
});

// Somewhere else in your application
const delay2 = createDelayContext();

// Example of event
something.on('resize', () => {
    const cancel = delay2('throttled_resize_handler', 250, () => {
        console.log(`I'm doing some heavy calculations when something is resized`);
    });

    // You can cancel delayed function execution
    if(...){
        cancel();
    }
});

License

Release under the MIT license

Keywords

FAQs

Package last updated on 22 Oct 2022

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc