2.0.2 • Published 2 years ago

delay-me v2.0.2

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
2 years ago

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

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago