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 a duration of 0. If the duration is statically known to be 0, consider using run.
Frequently the duration is either a constant or computed as in the following example (taking advantage of the multiplication operator of the Duration class):
void main() {
scheduleTimeout(5 * 1000); // 5 seconds.
}
Timer scheduleTimeout([int milliseconds = 10000]) =>
Timer(Duration(milliseconds: milliseconds), 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:
callback asynchronously as soon as possible.
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-async/Timer-class.html