Timer 计时器构造函数
- Duration duration,
- void callback(
创建一个新的计时器。
在给定的 duration
之后调用 callback
函数。
例子
final timer =
Timer(const Duration(seconds: 5), () => print('Timer finished'));
// Outputs after 5 seconds: "Timer finished".
实现
factory Timer(Duration duration, void Function() callback) {
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.createTimer(duration, callback);
}
return Zone.current
.createTimer(duration, Zone.current.bindCallbackGuarded(callback));
}