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 asDuration.zero.If the duration is statically known to be 0, consider usingrun.
void main() { Timer(const Duration(seconds: 5), handleTimeout);}void handleTimeout() { // callback function // Do some work.}Note: If Dart code usingTimer is compiled to JavaScript, the finestgranularity available in the browser is 4 milliseconds.
See also:
- Stopwatch for measuring elapsed time.
- Annotations
- @vmIsolateUnsendable
Constructors
- Timer.new(Durationduration,voidcallback())
- Creates a new timer.factory
- Timer.periodic(Durationduration,voidcallback(Timertimer))
- Creates a new repeating timer.factory
Properties
Methods
- cancel(
)→ void - Cancels the timer.
- noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- toString(
)→String - A string representation of this object.inherited
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited
Static Methods
- run(
voidcallback())→ void - Runs the given
callbackasynchronously as soon as possible.