Interface Function

Index

Methods

Methods

public after(num?: number): Function

Creates a function that will execute after [num] calls.

extra

%after% is useful for running a final callback after a series of asynchronous operations, when the order in which the operations will complete is unknown.

example

var fn = (function() { // Will be executed once only }).after(3); fn(); fn(); fn();

Parameters

  • num?: number optional

    Execute the function after this many calls, default = 1.

Returns

Function

Function that can only execute after it has been called times.

public bind(scope?: any, args?: Array<any>): Function

Binds as the %this% object for the function when it is called. Also allows currying an unlimited number of parameters.

extra

"currying" means setting parameters ([arg1], [arg2], etc.) ahead of time so that they are passed when the function is called later. If you pass additional parameters when the function is actually called, they will be added will be added to the end of the curried parameters. This method is provided for browsers that don't support it internally.

example

(function() { return this; }).bind('woof')(); -> returns 'woof'; function is bound with 'woof' as the this object. (function(a) { return a; }).bind(1, 2)(); -> returns 2; function is bound with 1 as the this object and 2 curried as the first parameter (function(a, b) { return a + b; }).bind(1, 2)(3); -> returns 5; function is bound with 1 as the this object, 2 curied as the first parameter and 3 passed as the second when calling the function

Parameters

  • scope?: any optional

    this object during the function call.

  • args?: Array<any> optional

    Curried parameters.

Returns

Function

Function with bound 'this' and curried parameters.

public cancel(): Function

Cancels a delayed function scheduled to be run.

extra

%delay%, %lazy%, %throttle%, and %debounce% can all set delays.

example

(function() { alert('hay'); // Never called }).delay(500).cancel();

Returns

Function

Function

public debounce(ms: number): Function

Creates a "debounced" function that postpones its execution until after milliseconds have passed.

extra

This method is useful to execute a function after things have "settled down". A good example of this is when a user tabs quickly through form fields, execution of a heavy operation should happen after a few milliseconds when they have "settled" on a field.

example

var fn = (function(arg1) { // called once 50ms later }).debounce(50); fn() fn() fn();

Parameters

  • ms: number

    Number of milliseconds to debounce the function.

Returns

Function

Deboucned function by ms .

public delay(ms?: number, args?: Array<any>): Function

Executes the function after milliseconds.

extra

Returns a reference to itself. %delay% is also a way to execute non-blocking operations that will wait until the CPU is free. Delayed functions can be canceled using the %cancel% method. Can also curry arguments passed in after .

example

(function(arg1) { // called 1s later }).delay(1000, 'arg1');

Parameters

  • ms?: number optional

    Milliseconds to delay execution, default = 0.

  • args?: Array<any> optional

    Additional arguments.

Returns

Function

Reference to itself.

public fill(args?: Array<any>): Function

Returns a new version of the function which when called will have some of its arguments pre-emptively filled in, also known as "currying".

extra

Arguments passed to a "filled" function are generally appended to the curried arguments. However, if %undefined% is passed as any of the arguments to %fill%, it will be replaced, when the "filled" function is executed. This allows currying of arguments even when they occur toward the end of an argument list (the example demonstrates this much more clearly).

example

var delayOneSecond = setTimeout.fill(undefined, 1000); delayOneSecond(function() { // Will be executed 1s later });

Parameters

  • args?: Array<any> optional

    Pre-filled arguments.

Returns

Function

Function with pre-filled arguments.

public lazy(ms?: number, limit?: number): Function

Creates a lazy function that, when called repeatedly, will queue execution and wait [ms] milliseconds to execute again.

method

lazy([ms] = 1, [limit] = Infinity)

extra

Lazy functions will always execute as many times as they are called up to [limit], after which point subsequent calls will be ignored (if it is set to a finite number). Compare this to %throttle%, which will execute only once per [ms] milliseconds. %lazy% is useful when you need to be sure that every call to a function is executed, but in a non-blocking manner. Calling %cancel% on a lazy function will clear the entire queue. Note that [ms] can also be a fraction.

example

(function() { // Executes immediately. }).lazy()(); (3).times(function() { // Executes 3 times, with each execution 20ms later than the last. }.lazy(20)); (100).times(function() { // Executes 50 times, with each execution 20ms later than the last. }.lazy(20, 50));

Parameters

  • ms?: number optional

    Wait this long between successive calls, default = 1.

  • limit?: number optional

    Maximum number of times the function can execute, default = Infinity.

Returns

Function

Function

public once(): Function

Creates a function that will execute only once and store the result.

extra

%once% is useful for creating functions that will cache the result of an expensive operation and use it on subsequent calls. Also it can be useful for creating initialization functions that only need to be run once.

example

var fn = (function() { // Will be executed once only }).once(); fn(); fn(); fn();

Returns

Function

Function

public throttle(ms: number): Function

Creates a "throttled" version of the function that will only be executed once per milliseconds.

extra

This is functionally equivalent to calling %lazy% with a [limit] of %1%. %throttle% is appropriate when you want to make sure a function is only executed at most once for a given duration. Compare this to %lazy%, which will queue rapid calls and execute them later.

example

(3).times(function() { // called only once. will wait 50ms until it responds again }.throttle(50));

Parameters

  • ms: number

    Execute only once in this time span.

Returns

Function

Function