API Reference 0.3.24dart_web_toolkit_utilTimer

Timer class

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 {@link #run}) and call {@link #schedule} or {@link #scheduleRepeating}.

NOTE: If you are using a timer to schedule a UI animation, use {@link com.google.gwt.animation.client.AnimationScheduler} instead. The browser can optimize your animation for maximum performance.

Example

{@example com.google.gwt.examples.TimerExample}

class Timer {
 static List<Timer> _timers = new List<Timer>();
 static bool _initialised = false;

 static void clearInterval(dart_async.Timer timer) {
   timer.cancel();
 }

 static void clearTimeout(dart_async.Timer timer) {
   timer.cancel();
 }

 static dart_async.Timer createInterval(Timer timer, int period) {
   return new dart_async.Timer.periodic(new Duration(milliseconds:period), (dart_async.Timer t){
     timer.fire();
   });
 }

 static dart_async.Timer createTimeout(Timer timer, int delay) {
   return new dart_async.Timer(new Duration(milliseconds:delay), (){
     timer.fire();
   });
 }

 static void _hookWindowClosing() {
   // Catch the window closing event.
   dart_html.window.onUnload.listen((dart_html.Event event) {
       while (_timers.length > 0) {
         _timers[0].cancel();
       }
   });
 }

 bool isRepeating = false;

 dart_async.Timer _timer;

 Function _callback;

 factory Timer.get(Function _callback) {
   if (!_initialised) {
     _initialised = true;
     _hookWindowClosing();
   }
   return new Timer._internal(_callback);
 }

 Timer._internal(this._callback);

 /**
  * Cancels this timer.
  */
 void cancel() {
   if (_timer != null) {
     if (isRepeating) {
       clearInterval(_timer); //timerId);
     } else {
       clearTimeout(_timer); //timerId);
     }
     int indx = _timers.indexOf(this);
     if (indx != -1) {
       _timers.removeAt(indx);
     }
   }
 }

 /**
  * This method will be called when a timer fires. Override it to implement the
  * timer's logic.
  */
 void run() {
   _callback();
 }

 /**
  * Schedules a timer to elapse in the future.
  *
  * @param delayMillis how long to wait before the timer elapses, in
  *          milliseconds
  */
 void schedule(int delayMillis) {
   if (delayMillis < 0) {
     throw new Exception("must be non-negative");
   }
   cancel();
   isRepeating = false;
   _timer = createTimeout(this, delayMillis);
   _timers.add(this);
 }

 /**
  * Schedules a timer that elapses repeatedly.
  *
  * @param periodMillis how long to wait before the timer elapses, in
  *          milliseconds, between each repetition
  */
 void scheduleRepeating(int periodMillis) {
   if (periodMillis <= 0) {
     throw new Exception("must be positive");
   }
   cancel();
   isRepeating = true;
   _timer = createInterval(this, periodMillis);
   _timers.add(this);
 }

 /*
  * Called by code when this timer fires.
  */
 void fire() {
   // If this is a one-shot timer, remove it from the timer list. This will
   // allow it to be garbage collected.
   if (!isRepeating) {
     int indx = _timers.indexOf(this);
     if (indx != -1) {
       _timers.removeAt(indx);
     }
   }

   // Run the timer's code.
   run();
 }
}

Static Methods

void clearInterval(Timer timer) #

static void clearInterval(dart_async.Timer timer) {
 timer.cancel();
}

void clearTimeout(Timer timer) #

static void clearTimeout(dart_async.Timer timer) {
 timer.cancel();
}

Timer createInterval(Timer timer, int period) #

static dart_async.Timer createInterval(Timer timer, int period) {
 return new dart_async.Timer.periodic(new Duration(milliseconds:period), (dart_async.Timer t){
   timer.fire();
 });
}

Timer createTimeout(Timer timer, int delay) #

static dart_async.Timer createTimeout(Timer timer, int delay) {
 return new dart_async.Timer(new Duration(milliseconds:delay), (){
   timer.fire();
 });
}

Constructors

factory Timer.get(Function _callback) #

factory Timer.get(Function _callback) {
 if (!_initialised) {
   _initialised = true;
   _hookWindowClosing();
 }
 return new Timer._internal(_callback);
}

Properties

bool isRepeating #

bool isRepeating = false

Methods

void cancel() #

Cancels this timer.

void cancel() {
 if (_timer != null) {
   if (isRepeating) {
     clearInterval(_timer); //timerId);
   } else {
     clearTimeout(_timer); //timerId);
   }
   int indx = _timers.indexOf(this);
   if (indx != -1) {
     _timers.removeAt(indx);
   }
 }
}

void fire() #

void fire() {
 // If this is a one-shot timer, remove it from the timer list. This will
 // allow it to be garbage collected.
 if (!isRepeating) {
   int indx = _timers.indexOf(this);
   if (indx != -1) {
     _timers.removeAt(indx);
   }
 }

 // Run the timer's code.
 run();
}

void run() #

This method will be called when a timer fires. Override it to implement the timer's logic.

void run() {
 _callback();
}

void schedule(int delayMillis) #

Schedules a timer to elapse in the future.

@param delayMillis how long to wait before the timer elapses, in

     milliseconds
void schedule(int delayMillis) {
 if (delayMillis < 0) {
   throw new Exception("must be non-negative");
 }
 cancel();
 isRepeating = false;
 _timer = createTimeout(this, delayMillis);
 _timers.add(this);
}

void scheduleRepeating(int periodMillis) #

Schedules a timer that elapses repeatedly.

@param periodMillis how long to wait before the timer elapses, in

     milliseconds, between each repetition
void scheduleRepeating(int periodMillis) {
 if (periodMillis <= 0) {
   throw new Exception("must be positive");
 }
 cancel();
 isRepeating = true;
 _timer = createInterval(this, periodMillis);
 _timers.add(this);
}