Home

Awesome

Introduction

The CountdownTimer is an easy to use library for performing timed events. It allows the user to configure the timer's duration and interval at which tick events will occur. The timer will trigger an onTickEvent() after each tick interval and will eventually trigger an onFinishEvent() when the whole timer duration has finished. In order to use the library, the main Processing applet will have to implement the two callback events:

Detailed examples of using the CountdownTimer can be found inside the bundled example codes.

Installing the Library

The official method for installing Processing libraries can be found here. In case the library needs to be downloaded manually, you can download it from here.

How It Works

Operation Behavior

Once a configured timer starts, the first onTickEvent() will be triggered after a period of tick interval has passed. Subsequent onTickEvent() will follow until the time left is equal or less than the tick interval. If there is no more time left for an onTickEvent() to happen, the onFinishEvent() will be triggered as soon as the timer runs out of remaining time.

If the onTickEvent() operation runs longer than the tick interval, the subsequent onTickEvent() that was supposed to be triggered under normal circumstances will be skipped.

Operation States

The timer has two booleans for representing its states: isRunning and isPaused. This was intended in order to differentiate a not-running timer that has never started yet and a not-running timer that was stopped in the middle.

By using the two booleans, there can be three possible states.

A state consisting isRunning() == true && isPaused == true cannot happen, since a timer cannot simultaneously stop and run at the same time.

As a summary, each boolean can be used for checking the following conditions:

Using the Library

Creating a Timer

All CountdownTimer are created and managed through the CountdownTimerService. A CountdownTimer should be created using the static factory method CountdownTimerService.getNewCountdownTimer(PApplet). Several examples of creating a CountdownTimer are:

// creates a new timer that has not been configured and started
CountdownTimer timer = CountdownTimerService.getNewCountdownTimer(this);

// creates a new timer that has been configured to trigger ticks every 1000 ms and run for a total of 5000 ms
CountdownTimer timer = CountdownTimerService.getNewCountdownTimer(this).configure(1000, 5000);

// creates and configures a new timer and starts it right away
CountdownTimer timer = CountdownTimerService.getNewCountdownTimer(this).configure(1000, 5000).start();

Using a Timer

A CountdownTimer MUST be configured at least once before being started, or else an IllegalStateException will be thrown. It won't make sense for a CountdownTimer to start when it doesn't know how long it should run.

The following methods are available for each timer:

A CountdownTimer can be stopped or reset with different behaviors based on the StopBehavior type being used:

Using Multiple Timers

Multiple timers can run inside the applet, in which case the timers will run independently of each other. Individual timers can be referred to by either:

Managing All Timers Through CountdownTimerService

The CountdownTimerService provides methods for managing all CountdownTimer that have been created:

Implementing Callback Events

The following callback events need to be implemented inside the main Processing applet in order for the library to be used properly:

NOTICE: The legacy callbacks void onTickEvent(int timerId, long timeLeftUntilFinish) and void onTickEvent(int timerId, long timeLeftUntilFinish) will still function for backwards compatibility, but it is recommended to use the new callbacks that were added to v0.9.3 for better method chaining.

Additional Info

Any other details about how to use this can be found inside the reference folder from the downloaded library. The library does not require any other dependencies to run, and it has been tested with the following environments: