Package com.google.gwt.user.client
Class Timer
java.lang.Object
com.google.gwt.user.client.Timer
A simplified, browser-safe timer class. This class serves the same purpose as
java.util.Timer, but is simplified because of the single-threaded
environment.
To schedule a timer, simply create a subclass of it (overriding run())
and call schedule(int) or scheduleRepeating(int).
NOTE: If you are using a timer to schedule a UI animation, use
AnimationScheduler instead. The
browser can optimize your animation for maximum performance.
Example
public class TimerExample implements EntryPoint, ClickHandler {
public void onModuleLoad() {
Button b = new Button("Click and wait 5 seconds");
b.addClickHandler(this);
RootPanel.get().add(b);
}
public void onClick(ClickEvent event) {
// Create a new timer that calls Window.alert().
Timer t = new Timer() {
@Override
public void run() {
Window.alert("Nifty, eh?");
}
};
// Schedule the timer to run once in 5 seconds.
t.schedule(5000);
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels this timer.(package private) final voidfire(int scheduleCancelCounter) final booleanReturnstrueif the timer is running.abstract voidrun()This method will be called when a timer fires.voidschedule(int delayMillis) Schedules a timer to elapse in the future.voidscheduleRepeating(int periodMillis) Schedules a timer that elapses repeatedly.
-
Constructor Details
-
Timer
public Timer()
-
-
Method Details
-
isRunning
public final boolean isRunning()Returnstrueif the timer is running. Timer is running if and only if it is scheduled but it is not expired or cancelled. -
cancel
public void cancel()Cancels this timer. If the timer is not running, this is a no-op. -
run
public abstract void run()This method will be called when a timer fires. Override it to implement the timer's logic. -
schedule
public void schedule(int delayMillis) Schedules a timer to elapse in the future. If the timer is already running then it will be first canceled before re-scheduling.- Parameters:
delayMillis- how long to wait before the timer elapses, in milliseconds
-
scheduleRepeating
public void scheduleRepeating(int periodMillis) Schedules a timer that elapses repeatedly. If the timer is already running then it will be first canceled before re-scheduling.- Parameters:
periodMillis- how long to wait before the timer elapses, in milliseconds, between each repetition
-
fire
final void fire(int scheduleCancelCounter)
-