Class: Function

Function

The JavaScript Function object.

new Function()

Source:
See:

Methods

delay(delay, args, thisArg) → {Object}

Delays a function call for the specified number of milliseconds.

Parameters:
Name Type Argument Default Description
delay Number

The number of milliseconds to wait before calling the functions.

args Array <optional>

An array containing the arguments the function will be called with.

thisArg Object <optional>
returned callback object

An object you want this to refer to inside the function. Defaults to the object returned by this function. If thisArg is an Array, args must be present (but may be null).

Source:
See:
Returns:
Type
Object

An object with the following properties:

fn
Type: Function
The function on which this method was called.
hasExecuted
Type: Boolean
A Boolean, initialized to false, that is set to true when the delayed function executes.
exec
Type: Function(Boolean cancel)
A function that, when called, will execute the function immediately and cancel the timeout so it is not called again by the browser. To prevent the timeout from being cancelled, call this function with the parameter false.
cancel
Type: Function
A function that, when called, will cancel the timeout to prevent the function from being executed (if it hasn't been already).

Examples

Call a function at a later time:

window.alert.delay(2000, ['alert!']); // Waits 2 seconds, then opens an alert that says "alert!"

Set a timeout for a function but cancel it before it can be called:

var ref = window.alert.delay(2000, ['alert!']); // Sets the alert to be called in 2 seconds and
                                               // saves a reference to the returned object
ref.fn === window.alert; // -> true (just to show what `fn` in the reference object is set to)

//----- Before 2 seconds ellapses -----
ref.cancel(); // Prevents the alert function from being called

every(delay, args, thisArg) → {Object}

Executes the function repeatedly, with a fixed time delay between each call to the function.

Parameters:
Name Type Argument Default Description
delay Number

The number of milliseconds to wait between function calls.

args Array <optional>

An array containing the arguments the function will be called with.

thisArg Object <optional>
returned callback object

An object you want this to refer to inside the function. Defaults to the object returned by this function. If thisArg is an Array, args must be present (but may be null).

Source:
See:
Returns:
Type
Object

An object with the following properties:

fn
Type: Function
The function on which this method was called.
hasExecuted
Type: Boolean
A Boolean, inialized to false, that is set to true each time the function executes.
exec
Type: Function(Boolean cancel)
A function that, when called, will execute the function immediately and cancel the interval so the function will stop being called. To prevent the interval from being cancelled, call this function with the parameter false.
cancel
Type: Function
A function that, when called, will cancel the interval so the function will stop being called.

Example

Set a function to repeat every 2 seconds and later stop it from continuing:

function logStuff() {
   console.log('stuff');
}

var ref = logStuff.every(2000); // Logs "stuff" to the console and every 2 seconds
                               // and saves a reference to the returned object
ref.fn === logStuff; // -> true (just to show what `fn` in the reference object is set to)

//----- Later -----
ref.cancel(); // Stops the logging calls