Class OorianTimer
TimerEvent instances on a schedule.
OorianTimer uses a ScheduledExecutorService internally for scheduling and integrates
with OorianSession so that timer callbacks have full access to the session context,
allowing them to modify elements and call sendUpdate().
Repeating timer:
OorianTimer timer = new OorianTimer(5000); // 5 second interval
timer.registerListener(this);
timer.start();
Repeating timer with initial delay:
OorianTimer timer = new OorianTimer(5000);
timer.setInitialDelay(2000); // wait 2 seconds before first tick
timer.registerListener(this);
timer.start();
One-shot timer:
OorianTimer timer = new OorianTimer(3000, false); // fires once after 3 seconds
timer.registerListener(this);
timer.start();
Timers are automatically stopped when their session is invalidated or destroyed.
For page-scoped timers, stop the timer in onUnloaded() as a best practice.
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionOorianTimer(long interval) Creates a new repeating timer with the specified interval.OorianTimer(long interval, boolean repeating) Creates a new timer with the specified interval and repeat behavior. -
Method Summary
Modifier and TypeMethodDescriptionlongReturns the interval in milliseconds.intReturns the number of times this timer has fired.booleanReturns whether this timer repeats.voidregisterListener(TimerListener listener) Registers a listener to receiveTimerEventnotifications.voidsetInitialDelay(long initialDelay) Sets the initial delay before the first tick.voidstart()Starts the timer.voidstop()Stops the timer.
-
Constructor Details
-
OorianTimer
public OorianTimer(long interval) Creates a new repeating timer with the specified interval.- Parameters:
interval- the interval between ticks in milliseconds
-
OorianTimer
public OorianTimer(long interval, boolean repeating) Creates a new timer with the specified interval and repeat behavior.- Parameters:
interval- the interval in milliseconds (for repeating timers) or delay (for one-shot timers)repeating-truefor a repeating timer,falsefor a one-shot timer
-
-
Method Details
-
setInitialDelay
public void setInitialDelay(long initialDelay) Sets the initial delay before the first tick.If not set, the default is 0 for repeating timers (fires immediately) and
intervalfor one-shot timers.- Parameters:
initialDelay- the initial delay in milliseconds
-
registerListener
Registers a listener to receiveTimerEventnotifications.- Parameters:
listener- the listener to register
-
start
public void start()Starts the timer.Captures the current
OorianSessionand registers this timer with the session for lifecycle management. The timer begins scheduling ticks according to its configuration.For repeating timers, ticks fire at a fixed rate using
ScheduledExecutorService.scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit). For one-shot timers, a single tick is scheduled usingScheduledExecutorService.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit). -
stop
public void stop()Stops the timer.Cancels any pending or repeating ticks and shuts down the executor. A stopped timer cannot be restarted.
-
getInterval
public long getInterval()Returns the interval in milliseconds.- Returns:
- the timer interval
-
isRepeating
public boolean isRepeating()Returns whether this timer repeats.- Returns:
trueif this is a repeating timer,falsefor one-shot
-
getTickCount
public int getTickCount()Returns the number of times this timer has fired.- Returns:
- the tick count
-