Target Methods and Callbacks

Every target in TargetJS supports optional properties or methods to control its values, timing, execution, and lifecycle.

1. Values and Activation

These properties determine what a target does and when it is eligible for execution.

  • value: Defines the target's desired value or a function that calculates it. When defined as a function, its return value becomes the target value.
  • initialValue: Sets the initial actual value before the target executes.
  • active: Controls whether the target is initially available for execution. Targets are active by default. Setting active: false requires the target to be activated before execution.
  • enabledOn: Determines whether an active target is eligible to execute. When it returns false, the execution is deferred.
  • pauseOn: Determines whether an ongoing target transition should be paused.

2. Timing, Animation, and Repetition

Targets have built-in controls for managing the progression and repetition of their execution. These controls work together to define animation timing and repeated operations.

  • steps: Specifies the number of iterations used to transition the actual value toward the target value.
  • interval: Defines the interval in milliseconds between animation updates or repeated executions.
  • easing: Controls the progression of the actual value during an animation, allowing transitions to accelerate, decelerate, or follow other easing curves.
  • cycles: Specifies how many times a target executes within a sequence.
  • loop: Controls whether the target repeats its execution. When set to true, the target continuously repeats its execution. When set to 'passive', the target is evaluated only when TargetJS runs a new processing cycle, such as one triggered by a window resize, rather than scheduling its own repeated executions.

For example, the following target animates its width between 100 and 300 pixels using 30 steps, with a 50-millisecond interval between updates:

width: {
        value: [100, 300, 100],
        steps: 30,
        interval: 50,
        easing: Easing.inOutQuad
    }

The easing property changes how the actual width progresses through each transition, while steps and interval control the timing.

3. Lifecycle Callbacks

Lifecycle callbacks allow developers to respond to different stages of a target's execution. They are invoked automatically by TargetJS and can be used independently or together.

  • onUpdate(updatedTModel, updatedTargetName): Called whenever the target produces an execution update, including updates from imperative operations it initiates on itself or other TModels. updatedTModel references the TModel whose target was updated, while updatedTargetName identifies the updated target.
  • onValueChange(value): Called whenever the target's actual value changes, including during transitions. The value argument contains the newly updated value.
  • onComplete(): Called when the target has finished its execution, including all dependent operations and imperative targets it initiated. This callback does not receive any arguments.

4. API Callbacks

  • onSuccess(): Called when an API request completes successfully. It receives the response so the result can be processed.
  • onError(): Called when an API request fails, allowing the target to handle the error.

Both callbacks are optional. The onValueChange() callback is also invoked when an API request succeeds or fails, provided the target's actual value has changed.

Documentation topics