fcronjs
v2.0.1
Published
Package for manage function call timetable
Downloads
2
Readme
fcronjs
Package for manage function call timetable.
WARNING:
Since version 2.0.1 the contract for debounce and throttle methods has been changed. New contract is compatible for old throttle's contract but incompatible for debounce's contract in previous versions.
Content
GitHub.
Install.
npm install fcronjs --save
Usage.
Import fcronjs
form package:
import fcronjs from 'fcronjs';
or add script fcron.js from web/fcron.js in branch master and add itinto your HTML:
<script src="./fcron.js"></script>
and use:
debounce
import { debounce } from 'fcronjs';
or
var debounce = fcronjs.debounce;
Method debounce creates Hi Ordered Function which sets minimal period between calls. It has two arguments:
{Function} func - original function.
{Object|number} [secondArgument] - in case Object - configurin object, in other case - [config#delay = 100].
Configuring object has 3 parameters:
- {number} [config#delay = 100] - minimal number of milliseconds to be waited between calls.
- {boolean} [config#immediate = false] - when immediate = true function calls immediatly if it is possible, in other case it cals after delay ms.
- {Object} [config#context] - context object of function. If context was set it can not be changed or removed.
For examlpe:
import { debounce } from 'fcronjs';
const f = debounce(console.log, {delay: 1000, immediate: false, context: console});
const timeouts = [0, 10, 100, 1000, 1010, 2000, 3000, 3500, 3550, 4200];
timeouts.forEach(x => setTimeout(f, x, x));
In output very likely will be 0
, 1010
, 3000
, and 4200
. And 10
, 100
, 1000
, 2000
, 3500
likely to be ignored.
throttle
import { throttle } from 'fcronjs';
or
var throttle = fcronjs.throttle;
Method throttle creates Hi Ordered Function which sets minimal period between calls and execute last call every time at the end.
{Function} func - original function.
{Object|number} [secondArgument] - in case Object - configurin object, in other case - [config#delay = 100].
Configuring object has 3 parameters:
- {number} [config#delay = 100] - minimal number of milliseconds to be waited between calls.
- {boolean} [config#immediate = false] - when immediate = true function calls immediatly if it is possible, in other case it cals after delay ms.
For examlpe:
import { throttle } from 'fcronjs';
const f = throttle(console.log, 1000);
const timeouts = [0, 10, 100, 1000, 1010, 2000, 3000, 3500, 3550, 10000, 10001];
timeouts.forEach(x => setTimeout(f, x, x));
In output very likely will be 0
, 1000
, 1010
(as a last), 2000
, 3000
, and 3550
(as a last), 10000
, and 10001
(as a last). And 10
, 100
, 3500
likely to be ignored.
waiter
import { waiter } from 'fcronjs';
or
var waiter = fcronjs.waiter;
Method waiter creates Hi Ordered Function which at call waits some time befor call original function, and in case of second call before function called, it stops waiting without calling function.
- {Function} func - original function.
- {number} [delay = 100] - number of milliseconds must be waited before original function call.
For examlpe:
import { waiter } from 'fcronjs';
const DELAY = 1000;
const f = waiter(console.log, DELAY);
f('Executing function must be called'); // will be executed after 1 second
setTimeout(() => {
f('Executing function must be ignored'); // Will never be executed
setTimeout(() => {
f(); // Canceling call after 500 ms.
}, DELAY / 2);
}, 5 * DELAY);
Support.
Supported browsers IE9+.
License
MIT Copyright (c) 2017 Kuznetsov Leonid.