Function(al) utilities.
Returns a function that ensures that fn
is only called after delay
milliseconds have elapsed. When the returned function is called before the
delay has elapsed, it will wait until the delay has elapsed and then call fn
.
When the returned function is called after the delay has elapsed, it will call
fn
after the next "tick" (it will always be called asynchronously). The
context and arguments that the returned function is called in are applied to
fn
.
In the below example onLoaded
will not be executed before a 1000 millisecond
delay. Even if loadImages
loads and calls callback
earlier. However, say
the images take 1500 milliseconds to load, it will trigger onLoaded
immediately.
var callback = after(onLoaded, 1000);
loadImages(callback);
function onLoaded(){
console.log('loaded');
}
You can also cancel de delayed call by simply using the native clearTimeout
method (like a regular setTimeout
call).
var timeoutId = callback();
// onLoaded won't be called since it was canceled before the 1000ms delay
clearTimeout(timeoutId);
fn
(Function) : Target Functiondelay
(Number) : Delay of execution in millisecondsSee: debounce()
Return a function that will execute in the given context, optionally adding any additional supplied parameters to the beginning of the arguments collection.
fn
(Function) : Target Functioncontext
(Object) : Execution context (object used as this
)[...args]
(*) : Arguments (0...n arguments)See: partial()
, object/bindAll
Returns the composition of a list of functions, where each function consumes
the return value of the function that follows. In math terms, composing the
functions f()
, g()
, and h()
produces f(g(h()))
.
function add2(x) { return x + 2 }
function multi2(x) { return x * 2 }
map([1, 2, 3], compose(add2, multi2)); // [4, 6, 8]
//same as
map([1, 2, 3], function(x){
return add2( multi2(x) );
});
Returns a new function that will always return value
when called.
var f = constant('foo');
f(); // 'foo'
// Provided arguments are ignored; value is always returned
f(1); // 'foo'
f = constant({ foo: 'bar' });
f(); // { foo: 'bar' }
Creates a function that will delay the execution of fn
until after delay
milliseconds have elapsed since the last time it was invoked.
Subsequent calls to the debounced function will return the result of the last
fn
call.
// sometimes less is more
var lazyRedraw = debounce(redraw, 300);
foo.on.resize.add(lazyRedraw);
In this visualization, |
is a debounced-function call and X
is the actual
callback execution:
Default
||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
X X
Debounced with `isAsap == true`:
||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
X X
You also have the option to cancel the debounced call if it didn't happen yet:
lazyRedraw();
// lazyRedraw won't be called since `cancel` was called before the `delay`
lazyRedraw.cancel();
See: throttle()
Returns a function that calls a method with given name
on supplied object.
Useful for iteration methods like array/map
and array/forEach
.
See: prop()
// will call the method `getName()` for each `user`
var names = map(users, func('getName'));
Returns the first argument provided to it.
identity(3); // 3
identity(1,2,3); // 1
identity('foo'); // "foo"
Return a partially applied function supplying default arguments.
This method is similar to bind
, except it does not alter the this
binding.
fn
(Function) : Target Function[...args]
(*) : Arguments (0...n arguments)See: bind()
function add(a, b){ return a + b }
var add10 = partial(add, 10);
console.log( add10(2) ); // 12
Returns a function that gets a property with given name
from supplied object.
Useful for using in conjunction with array/map
and/or for creating getters.
See: array/pluck()
var users = [{name:"John", age:21}, {name:"Jane", age:25}];
// ["John", "Jane"]
var names = map(users, prop('name'));
Returns a function that will execute all the supplied functions in order and
passing the same parameters to all of them. Useful for combining multiple
array/forEach
into a single one and/or for debugging.
// call `console.log()` and `doStuff()` for each item item in the array
forEach(arr, series(console.log, doStuff));
Creates a function that, when executed, will only call the fn
function at
most once per every interval
milliseconds.
If the throttled function is invoked more than once during the wait timeout,
fn
will also be called on the trailing edge of the timeout.
Subsequent calls to the throttled function will return the result of the last
fn
call.
// sometimes less is more
var lazyRedraw = throttle(redraw, 300);
foo.on.resize.add(lazyRedraw);
In this visualization, |
is a throttled-function call and X
is the actual
fn
execution:
||||||||||||||||||||||||| (pause) |||||||||||||||||||||||||
X X X X X X X X X X X X
You also have the option to cancel the throttled call if it didn't happen yet:
lazyRedraw();
setTimeout(function(){
lazyRedraw();
// lazyRedraw will be called only once since `cancel` was called before
// the `interval` for 2nd call completed
lazyRedraw.cancel();
}, 250);
See: debounce()
Functions as a wrapper for setTimeout
. Calls a the function fn
after a given delay millis
in milliseconds.
The function is called within the specified context. The return value can be used to clear the timeout using clearTimeout
.
var id = timeout(doStuff, 300, this);
clearTimeout(id);
Iterates over a callback n
times.
n
(Number) : Number of iterationscallback
(Function) : Closure executed for every iterationcontext
(Object) : Execution context (object used as this
)var output = '';
times(5, function(i) {
output += i.toString();
});
// output: 01234
For more usage examples check specs inside /tests
folder. Unit tests are the
best documentation you can get...