Class: ThrottledCallback

PD. ThrottledCallback


new ThrottledCallback(callback, delay [, payload])

Creates a new restartable delayed callback object.

This type of object is primarily used to limit how often a complex dynamic interaction or calculation can update the model or refresh the user interface. These objects are useful as they are persistent and can be re-triggered, cancelled and/or restarted at any time.

They can be used for relatively frequent updates (using a small delay of something like 50ms for 20fps) when dragging an interactive slider, or for much less frequent updates (using a long delay of something like 5 seconds) when a large grid needs to show progress as it recalculates, for example.

Parameters:
Name Type Argument Description
callback function

A function to call after the delay period.

delay number

The delay before calling in milliseconds, defaults to 50ms.

payload object <optional>

Optional data to be sent as the callback's argument.

Author:
  • drajmarsh
Throws:

Throws an error if callback is not a function.

Type
TypeError

Methods


cancel()

Cancels current timer if has not already timed out.

Returns:

Returns this instance to support method chaining.

Type
object

restart( [new_delay])

Restarts the throttled callback regardless of previous state.

Parameters:
Name Type Argument Description
new_delay number <optional>

The new delay in milliseconds, defaults to the original.

Returns:

Returns this instance to support method chaining.

Type
object

timeout( [payload])

Invokes the callback function with optional payload.

NOTE: This will also cancel the current timer if not already timed out.

Parameters:
Name Type Argument Description
payload object <optional>

Optional new data to be sent as the callback's argument.

Returns:

Returns this instance to support method chaining.

Type
object

trigger()

Starts the throttled callback if not already started.

Returns:

Returns this instance to support method chaining.

Type
object

waitUntilCallsStop( [state])

Get or set whether or not to invoke the callback continuously after each delay, or wait until all triggers stop for the given period before invoking.

To retrieve the current value without setting it, call this method without an argument. If you provide an argument of any kind, it will be converted to a boolean and set as the new value.

Parameters:
Name Type Argument Description
state boolean <optional>

The new state for waiting, defaults to false.

Returns:

Returns the current state of this value.

Type
boolean