Skip to content

Timer

Timer Class

The Timer class is responsible for managing time-based tasks and tweening values over a specific duration in a game or application. It tracks tasks that execute functions at set intervals or after a delay.

Constructor

new Timer();

Creates a new Timer instance with an empty list of tasks.

  • Returns: A new Timer object.

Properties

  • tasks (Array): An array of Task objects that are scheduled to run.

Methods

update(dt)

update(dt);

Updates all tasks, advancing their timers by the given delta time. Removes any tasks that have completed.

  • Parameters:

    • dt (Number): The time elapsed since the last update, in seconds.
  • Returns: void

addTask(action, interval, duration = 0, callback = () => {})

addTask(action, interval, (duration = 0), (callback = () => {}));

Adds a task to the Timer’s task list. The task will execute an action at a set interval and optionally for a set duration.

  • Parameters:

    • action (Function): The function to execute at each interval or for each frame.
    • interval (Number): The time between each execution of the action.
    • duration (Number, optional): The total time the task should run for, default is 0 (runs indefinitely).
    • callback (Function, optional): The function to execute after the duration has passed.
  • Returns: void

updateTasks(dt)

updateTasks(dt);

Updates all tasks by advancing their timers and executing their actions if their interval has elapsed.

  • Parameters:

    • dt (Number): The time elapsed since the last update, in seconds.
  • Returns: void

removeFinishedTasks()

removeFinishedTasks();

Removes tasks that have completed from the tasks list.

  • Returns: void

clear()

clear();

Clears all tasks from the timer.

  • Returns: void

tween(object, parameters, duration, easing = Easing.linear, callback = () => {})

tween(
object,
parameters,
duration,
(easing = Easing.linear),
(callback = () => {})
);

Tweens (interpolates) one or more properties of an object from their current value to a specified end value over a set duration.

  • Parameters:

    • object (Object): The object containing properties to be tweened.
    • parameters (Object): An object specifying the target values for properties to be tweened (e.g., { x: 100 }).
    • duration (Number): The duration of the tween in seconds.
    • easing (Function, optional): The easing function to use, default is Easing.linear.
    • callback (Function, optional): The function to execute after the tween completes.
  • Returns: void

tweenAsync(object, parameters, duration, easing = Easing.linear)

tweenAsync(object, parameters, duration, (easing = Easing.linear));

Performs a tween asynchronously, returning a Promise that resolves when the tween is complete.

  • Parameters:

    • object (Object): The object containing properties to be tweened.
    • parameters (Object): The target values for properties to be tweened.
    • duration (Number): The duration of the tween in seconds.
    • easing (Function, optional): The easing function to use, default is Easing.linear.
  • Returns: Promise: Resolves when the tween is complete.

wait(duration)

wait(duration);

Creates a delay that resolves after the specified duration using a Promise.

  • Parameters:

    • duration (Number): The duration of the wait in seconds.
  • Returns: Promise: Resolves when the wait time has elapsed.

Example Usage

const timer = new Timer();
// Add a task to log every 1 second for 5 seconds
timer.addTask(
() => console.log('Task running'),
1, // interval
5, // duration
() => console.log('Task complete')
);
// Tween an object's x property from 0 to 100 over 2 seconds
const obj = { x: 0 };
timer.tween(obj, { x: 100 }, 2);
// Asynchronous tween
await timer.tweenAsync(obj, { x: 100 }, 2);

Task Class

The Task class represents an action that will be executed at a specific interval or for a specific duration within the Timer.

Constructor

new Task(action, interval, (duration = 0), (callback = () => {}));

Creates a new Task instance that executes an action at a given interval and optionally for a set duration.

  • Parameters:

    • action (Function): The function to execute at each interval.
    • interval (Number): The time between each execution of the action.
    • duration (Number, optional): The total time the task should run for, default is 0 (runs indefinitely).
    • callback (Function, optional): The function to execute after the duration has passed.
  • Returns: A new Task object.

Properties

  • action (Function): The action to be performed at each interval.

  • interval (Number): The time between each action execution.

  • duration (Number): The total time the task will run.

  • intervalTimer (Number): The current time elapsed since the last action was executed.

  • totalTime (Number): The total time the task has been running.

  • callback (Function): The function to call when the task’s duration has completed.

  • isDone (Boolean): A flag indicating whether the task has finished.

Methods

update(dt)

update(dt);

Updates the task by incrementing the timers and executing the action if the interval has been reached. If the duration is reached, the callback is executed, and the task is marked as complete.

  • Parameters:

    • dt (Number): The time elapsed since the last update, in seconds.
  • Returns: void

Example Usage

const task = new Task(
() => console.log('Task executed'),
1,
5,
() => console.log('Task complete')
);
// Updating the task with delta time
task.update(1); // Executes the action every 1 second

Notes

  • The Timer class uses Task objects to schedule actions that can be repeated at specific intervals.
  • The tween and wait methods leverage tasks to perform animations or delays.
  • Tasks can either run for a defined duration or repeat indefinitely when no duration is set.