Class SimpleTimer
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasSize,HasStyle,Serializable
The state of the timer is kept on the server, as the time banked from previous runs plus the instant the current run started. The browser only renders it. When the component is detached and attached again, the elapsed time is pushed to the new client element, so the timer keeps counting while it is not in the DOM.
- Author:
- Leonardo Scardanzan / Flowing Code
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumWhat the timer does while it is detached.static classEvent fired when the timer reaches its limit.static classEvent fired periodically while the timer is running. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddCurrentTimeChangeListener(PropertyChangeListener listener, long period, TimeUnit periodUnit) Deprecated.addDisplay(Component display) Shows the value of the timer inside another component as well.addTickListener(ComponentEventListener<SimpleTimer.TimerTickEvent> listener, Duration period) Adds a listener that is notified while the timer is running, at most once per period.Deprecated.UseaddTimerEndListener(ComponentEventListener)instead.Adds a listener that is notified when the timer reaches its limit.Deprecated.UsegetElapsed()orgetRemaining()instead.Deprecated.UsegetElapsed()orgetRemaining()instead.Returns what the timer does while it is detached.Returns the time the timer has been running, up to its limit.getLimit()Returns the duration the timer counts down from, or counts up to.Returns the time left until the timer reaches its limit.booleanReturns whether the timer counts up.booleanReturns whether the timer is running.booleanprotected voidonAttach(AttachEvent attachEvent) protected voidonDetach(DetachEvent detachEvent) voidpause()Pauses the timer, keeping the elapsed time.voidreset()Resets the elapsed time to zero.voidsetCountdown(Duration duration) Configures the timer to count down from the given duration to zero, and resets it.voidsetCountUp(Duration limit) Configures the timer to count up from zero to the given limit, and resets it.voidsetDetachPolicy(SimpleTimer.DetachPolicy detachPolicy) Sets what the timer does while it is detached.voidsetDoubleDigitHours(boolean doubleDigitHours) Use two digits for hours.voidsetElapsed(Duration elapsed) Sets the elapsed time, for instance to restore a timer whose state was saved elsewhere.voidsetEndTime(Number endTime) Deprecated.UsesetCountUp(Duration)instead.voidsetFractions(boolean fractions) Enables showing fractions of a second.voidsetHours(boolean hours) Enables showing hours and minutes.voidsetMinutes(boolean minutes) Enables showing minutes.voidsetStartTime(Number startTime) Deprecated.UsesetCountdown(Duration)instead.voidsetVisible(boolean visible) Shows or hides the timer with CSS, so that it keeps running and communicating with the server while hidden.voidstart()Starts the timer.Methods inherited from class com.vaadin.flow.component.Component
addListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, onEnabledStateChanged, set, setElement, setIdMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
SimpleTimer
public SimpleTimer()Creates a timer.
-
-
Method Details
-
setCountdown
Configures the timer to count down from the given duration to zero, and resets it. A running timer keeps running from the new start.- Parameters:
duration- the duration to count down from- Throws:
IllegalArgumentException- ifdurationis negative
-
setCountUp
Configures the timer to count up from zero to the given limit, and resets it. A running timer keeps running from zero.- Parameters:
limit- the duration at which the timer stops- Throws:
IllegalArgumentException- iflimitis negative
-
isCountUp
public boolean isCountUp()Returns whether the timer counts up.- Returns:
trueif the timer counts up to its limit,falseif it counts down
-
getLimit
Returns the duration the timer counts down from, or counts up to.- Returns:
- the limit of the timer
-
start
public void start()Starts the timer. Does nothing if it is already running or has reached its limit. -
pause
public void pause()Pauses the timer, keeping the elapsed time. Does nothing if it is not running. -
reset
public void reset()Resets the elapsed time to zero. A running timer keeps running from the start. -
isRunning
public boolean isRunning()Returns whether the timer is running.- Returns:
trueif the timer has been started and has not reached its limit
-
getElapsed
Returns the time the timer has been running, up to its limit. The value is computed on the server and does not need a round trip.- Returns:
- the elapsed time
-
getRemaining
Returns the time left until the timer reaches its limit.- Returns:
- the remaining time
-
setElapsed
Sets the elapsed time, for instance to restore a timer whose state was saved elsewhere. A running timer keeps running from the given point.- Parameters:
elapsed- the elapsed time, clamped between zero and the limit
-
setFractions
public void setFractions(boolean fractions) Enables showing fractions of a second.- Parameters:
fractions- whether to show hundredths of a second
-
setMinutes
public void setMinutes(boolean minutes) Enables showing minutes.- Parameters:
minutes- whether to show minutes
-
setHours
public void setHours(boolean hours) Enables showing hours and minutes.- Parameters:
hours- whether to show hours and minutes
-
setDoubleDigitHours
public void setDoubleDigitHours(boolean doubleDigitHours) Use two digits for hours.- Parameters:
doubleDigitHours- true to format hours using two digits.
-
addTimerEndListener
public Registration addTimerEndListener(ComponentEventListener<SimpleTimer.TimerEndedEvent> listener) Adds a listener that is notified when the timer reaches its limit.If the timer reaches its limit while it is detached, the listener is notified when the component is attached again, and
SimpleTimer.TimerEndedEvent.isRetroactive()returnstrue.- Parameters:
listener- the listener to add- Returns:
- a handle that can be used for removing the listener
-
addTickListener
public Registration addTickListener(ComponentEventListener<SimpleTimer.TimerTickEvent> listener, Duration period) Adds a listener that is notified while the timer is running, at most once per period.Each notification is a round trip from the browser, so use a period that matches what the listener needs. To show the value in another component, use
addDisplay(Component)instead, which needs no round trips.- Parameters:
listener- the listener to addperiod- the minimum time between notifications- Returns:
- a handle that can be used for removing the listener
- Throws:
IllegalArgumentException- ifperiodis zero or negative
-
setDetachPolicy
Sets what the timer does while it is detached. The default isSimpleTimer.DetachPolicy.CONTINUE.- Parameters:
detachPolicy- the policy to apply when the timer is detached
-
getDetachPolicy
Returns what the timer does while it is detached.- Returns:
- the detach policy
-
addDisplay
Shows the value of the timer inside another component as well. The browser updates the value on every frame, without round trips to the server.The timer appends a text node to the component and only updates that node, so the content the component already has is kept. To show the value only inside other components, hide the timer with
setVisible(boolean): it keeps running while hidden.The value is only updated while the timer is attached. To keep a display updated while it is moved in and out of view, for instance inside tabs, attach the timer somewhere it stays attached and add only the display to the tab.
A component shows the value of one timer at a time: if it is added as a display to two timers, they overwrite each other's value. The timer keeps a reference to the component until the display is removed with the returned handle.
- Parameters:
display- the component in which to show the value- Returns:
- a handle that can be used for removing the display
-
onDetach
-
onAttach
-
isVisible
public boolean isVisible() -
setVisible
public void setVisible(boolean visible) Shows or hides the timer with CSS, so that it keeps running and communicating with the server while hidden. A hidden timer still updates the components added withaddDisplay(Component).- Overrides:
setVisiblein classComponent- Parameters:
visible- whether to show the timer
-
setStartTime
Deprecated.UsesetCountdown(Duration)instead.Sets the start time, for countdown mode.- Parameters:
startTime- value in seconds for the start time
-
setEndTime
Deprecated.UsesetCountUp(Duration)instead.Sets the end time, for countup mode.- Parameters:
endTime- value in seconds for the end time
-
getCurrentTime
Deprecated.UsegetElapsed()orgetRemaining()instead.Returns the displayed value of the timer: the remaining time when counting down, or the elapsed time when counting up.- Returns:
- current value in seconds
-
getCurrentTimeAsync
Deprecated.UsegetElapsed()orgetRemaining()instead.Returns the displayed value of the timer. The value is computed on the server, so the returned future is already completed.- Returns:
- a completed future with the value of
getCurrentTime()
-
addCurrentTimeChangeListener
@Deprecated public Registration addCurrentTimeChangeListener(PropertyChangeListener listener, long period, TimeUnit periodUnit) Deprecated.UseaddTickListener(ComponentEventListener, Duration)instead. The value itself is available on the server throughgetElapsed().Adds a property change listener for thecurrentTimeproperty.- Parameters:
listener- the property change listenerperiod- the minimum period between listener invocations, or 0 to disable throttlingperiodUnit- time duration of throttling period- Returns:
- this registration, for chaining
-
addTimerEndEvent
@Deprecated public Registration addTimerEndEvent(ComponentEventListener<SimpleTimer.TimerEndedEvent> listener) Deprecated.UseaddTimerEndListener(ComponentEventListener)instead.Adds a timer ended listener that will be triggered when the timer reaches its limit.- Parameters:
listener- the listener to add- Returns:
- a handle that can be used for removing the listener
-
addTickListener(ComponentEventListener, Duration)instead.