delay-me
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i delay-me

Weekly Downloads

1

Version

2.0.2

License

MIT

Unpacked Size

9.61 kB

Total Files

10

Last publish

Collaborators

  • jsnow0177