single-call
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

single-call!

A simple utility function to prevent having a function called several times, when you need it to run once.

(common use case: accounting for mouse and touch events)


const single = require('single-call');
var theLogFunction = () => {console.log('this is "theLogFunction" running\n\n');}
//
//
console.log('"single" used with the name "exampleName1" will be run once, then disabled\n');
setTimeout(() => {
    single('exampleName1', theLogFunction, 2400);
    single('exampleName1', theLogFunction, 2400);
    single('exampleName1', theLogFunction, 2400);
    single('exampleName1', theLogFunction, 2400);
    setTimeout(() => {
        single('exampleName1', theLogFunction, 2400);
        single('exampleName1', theLogFunction, 2400);
        single('exampleName1', theLogFunction, 2400);
        single('exampleName1', theLogFunction, 2400);
        single('exampleName1', theLogFunction, 2400);
        single('exampleName1', theLogFunction, 2400);
        setTimeout(() => {
            console.log('...then re-enabled when the "time" argument has expired\n');
            setTimeout(() => {
                single('exampleName1', theLogFunction, 2400);
            }, 1200)
        }, 1200)
    }, 600)
}, 1200);
 

output:

"single" used with the name "exampleName1" will be run once, then disabled

this is "theLogFunction" running


...then re-enabled when the "time" argument has expired

this is "theLogFunction" running

Dependents (0)

Package Sidebar

Install

npm i single-call

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

3.48 kB

Total Files

5

Last publish

Collaborators

  • ameola01