W3cubDocs

/Dart 2

Timer.periodic constructor

Timer.periodic(Duration duration, void callback(Timer timer))

Creates a new repeating timer.

The callback is invoked repeatedly with duration intervals until canceled with the cancel function.

The exact timing depends on the underlying timer implementation. No more than n callbacks will be made in duration * n time, but the time between two consecutive callbacks can be shorter and longer than duration.

In particular, an implementation may schedule the next callback, e.g., a duration after either when the previous callback ended, when the previous callback started, or when the previous callback was scheduled for - even if the actual callback was delayed.

Implementation

factory Timer.periodic(Duration duration, void callback(Timer timer)) {
  if (Zone.current == Zone.root) {
    // No need to bind the callback. We know that the root's timer will
    // be invoked in the root zone.
    return Zone.current.createPeriodicTimer(duration, callback);
  }
  var boundCallback = Zone.current.bindUnaryCallbackGuarded<Timer>(callback);
  return Zone.current.createPeriodicTimer(duration, boundCallback);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-async/Timer/Timer.periodic.html