ExpiringPeriodicTimer constructor
ExpiringPeriodicTimer({})
Constructor.
The ExpiringPeriodicTimer will expire after the total amount of time
has elapsed. The optional onFinish callback will be invoked upon
expiration.
Until the timer expires, onTick will be invoked periodically every
interval with the amount of time left before expiration.
Implementation
new({
required Duration total,
required this._interval,
required this._onTick,
this._onFinish,
}) : _endTime = clock.now().add(total) {
// Schedule the periodic [Timer] first to try to ensure that it fires first
// if it coincides with the completion [Timer].
_periodicTimer = Timer.periodic(_interval, (_) {
_onTick(_endTime.difference(clock.now()));
});
_completionTimer = Timer(total, () {
_periodicTimer.cancel();
_onFinish?.call();
});
}