public abstract class Timer
extends java.lang.Object
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.
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 and Description |
---|
Timer() |
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancels this timer.
|
(package private) void |
fire() |
abstract void |
run()
This method will be called when a timer fires.
|
void |
schedule(int delayMillis)
Schedules a timer to elapse in the future.
|
void |
scheduleRepeating(int periodMillis)
Schedules a timer that elapses repeatedly.
|
public void cancel()
public abstract void run()
public void schedule(int delayMillis)
delayMillis
- how long to wait before the timer elapses, in
millisecondspublic void scheduleRepeating(int periodMillis)
periodMillis
- how long to wait before the timer elapses, in
milliseconds, between each repetitionfinal void fire()