phoenix::Timer

Timers are used to trigger events every time a specific number of milliseconds have elapsed.

For a GUI application that needs to perform a given operation frequently, it is preferred to use a timer and OS::main(), rather than a while loop that calls OS::processEvents(). The latter will consume 100% CPU resources, and a usleep() inside of it will not process messages, causing the application to appear frozen.

Warning: Timer should not be used for high-frequency, high-precision applications. Many aspects are platform-dependant, such as: the minimum amount of milliseconds a timer will sleep for (on Windows this is 10ms), which timer will trigger first if two or more timers have an identical interval value, whether or not the time to execute the timer callback will factor into the timer interval for the next callback, and whether a timer will trigger again if the callback does not complete before the interval time elapses again.

struct Timer : Object {
  function<void ()> onTimeout;

  void setEnabled(bool enabled = true);
  void setInterval(unsigned milliseconds);
};

function<void ()> Timer::onTimeout;

Called every time the specified millisecond interval elapses.

void Timer::setEnabled(bool enabled = true);

Starts or stops a timer.

void Timer::setInterval(unsigned milliseconds);

Request that a timer trigger everytime the specified amount of milliseconds elapse.

Note that you can call this regardless of whether a timer is enabled or not. The enabled-state of the timer will not change, only its interval for future events will change.