Timer class abstract interface

A countdown timer that can be configured to fire once or repeatedly.

The timer counts down from the specified duration to 0. When the timer reaches 0, the timer invokes the specified callback function. Use a periodic timer to repeatedly count down the same interval.

A negative duration is treated the same as Duration.zero. If the duration is statically known to be 0, consider using run.

void main() {
  Timer(const Duration(seconds: 5), handleTimeout);
}

void handleTimeout() {  // callback function
  // Do some work.
}

Note: If Dart code using Timer is compiled to JavaScript, the finest granularity available in the browser is 4 milliseconds.

See also:

Annotations
  • @vmIsolateUnsendable

Constructors

Timer(Duration duration, void callback())
Creates a new timer.
factory
Timer.periodic(Duration duration, void callback(Timer timer))
Creates a new repeating timer.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
isActive bool
Returns whether the timer is still active.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tick int
The number of durations preceding the most recent timer event.
no setter

Methods

cancel() → void
Cancels the timer.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

run(void callback()) → void
Runs the given callback asynchronously as soon as possible.