Loading...
Searching...
No Matches
juce::Timer Class Referenceabstract

Detailed Description

Makes repeated callbacks to a virtual method at a specified time interval.

A Timer's timerCallback() method will be repeatedly called at a given interval. When you create a Timer object, it will do nothing until the startTimer() method is called, which will cause the message thread to start making callbacks at the specified interval, until stopTimer() is called or the object is deleted.

The time interval isn't guaranteed to be precise to any more than maybe 10-20ms, and the intervals may end up being much longer than requested if the system is busy. Because the callbacks are made by the main message thread, anything that blocks the message queue for a period of time will also prevent any timers from running until it can carry on.

If you need to have a single callback that is shared by multiple timers with different frequencies, then the MultiTimer class allows you to do that - its structure is very similar to the Timer class, but contains multiple timers internally, each one identified by an ID number.

See also
HighResolutionTimer, MultiTimer

The documentation for this class was generated from the following file:
Inheritance diagram for juce::Timer:

Public Member Functions

virtual ~Timer ()
 Destructor.
virtual void timerCallback ()=0
 The user-defined callback routine that actually gets called periodically.
void startTimer (int intervalInMilliseconds) noexcept
 Starts the timer and sets the length of interval required.
void startTimerHz (int timerFrequencyHz) noexcept
 Starts the timer with an interval specified in Hertz.
void stopTimer () noexcept
 Stops the timer.
bool isTimerRunning () const noexcept
 Returns true if the timer is currently running.
int getTimerInterval () const noexcept
 Returns the timer's interval.

Static Public Member Functions

static void JUCE_CALLTYPE callAfterDelay (int milliseconds, std::function< void()> functionToCall)
 Invokes a lambda after a given number of milliseconds.
static void JUCE_CALLTYPE callPendingTimersSynchronously ()
 For internal use only: invokes any timers that need callbacks.

Protected Member Functions

 Timer () noexcept
 Creates a Timer.
 Timer (const Timer &) noexcept
 Creates a copy of another timer.

Constructors and Destructors

◆ Timer() [1/2]

juce::Timer::Timer ( )
protectednoexcept

Creates a Timer.

When created, the timer is stopped, so use startTimer() to get it going.

Referenced by callPendingTimersSynchronously(), Timer(), and ~Timer().

◆ Timer() [2/2]

juce::Timer::Timer ( const Timer & )
protectednoexcept

Creates a copy of another timer.

Note that this timer won't be started, even if the one you're copying is running.

References Timer().

◆ ~Timer()

virtual juce::Timer::~Timer ( )
virtual

Destructor.

References Timer().

Member Functions

◆ timerCallback()

virtual void juce::Timer::timerCallback ( )
pure virtual

The user-defined callback routine that actually gets called periodically.

It's perfectly ok to call startTimer() or stopTimer() from within this callback to change the subsequent intervals.

Implemented in juce::BubbleMessageComponent, juce::ImagePreviewComponent, and juce::MidiKeyboardComponent.

References timerCallback().

Referenced by timerCallback().

◆ startTimer()

void juce::Timer::startTimer ( int intervalInMilliseconds)
noexcept

Starts the timer and sets the length of interval required.

If the timer is already started, this will reset it, so the time between calling this method and the next timer callback will not be less than the interval length passed in.

This method may be called from any thread. It only affects when future callbacks are scheduled. Callbacks themselves always run on the message thread.

Parameters
intervalInMillisecondsthe interval to use (any value less than 1 will be rounded up to 1)

References startTimer().

Referenced by juce::StandalonePluginHolder::init(), and startTimer().

◆ startTimerHz()

void juce::Timer::startTimerHz ( int timerFrequencyHz)
noexcept

Starts the timer with an interval specified in Hertz.

This is effectively the same as calling startTimer (1000 / timerFrequencyHz).

References startTimerHz().

Referenced by juce::AnimatedPosition< Behaviour >::endDrag(), juce::AnimatedPosition< Behaviour >::nudge(), and startTimerHz().

◆ stopTimer()

void juce::Timer::stopTimer ( )
noexcept

Stops the timer.

This method may be called from any thread. It cancels any future callbacks for this timer. If a callback is already running on the message thread, this method does not wait for it to finish and may return while that callback is still executing.

Do not destroy the Timer (or data it uses) from another thread while a callback may still be running. Before destroying a timer it's best practice to stop the timer from the message thread. If the Timer will be destroyed from a background thread consider using MessageManager::callSync() to call stopTimer() from the message thread.

References stopTimer().

Referenced by juce::AnimatedPosition< Behaviour >::beginDrag(), juce::AnimatedPosition< Behaviour >::setPosition(), stopTimer(), and juce::StandalonePluginHolder::~StandalonePluginHolder().

◆ isTimerRunning()

bool juce::Timer::isTimerRunning ( ) const
inlinenoexcept

Returns true if the timer is currently running.

References isTimerRunning().

Referenced by isTimerRunning().

◆ getTimerInterval()

int juce::Timer::getTimerInterval ( ) const
inlinenoexcept

Returns the timer's interval.

Returns
the timer's interval in milliseconds if it's running, or 0 if it's not.

◆ callAfterDelay()

void JUCE_CALLTYPE juce::Timer::callAfterDelay ( int milliseconds,
std::function< void()> functionToCall )
static

Invokes a lambda after a given number of milliseconds.

References JUCE_CALLTYPE.

◆ callPendingTimersSynchronously()

void JUCE_CALLTYPE juce::Timer::callPendingTimersSynchronously ( )
static

For internal use only: invokes any timers that need callbacks.

Don't call this unless you really know what you're doing!

References JUCE_CALLTYPE, and Timer().