startCountDown method

void startCountDown()

start countdown Timer. 启动倒计时Timer.

Remaining time is derived from a wall-clock end time captured once at start (endTime = now + mTotalTime), so the countdown stays in sync with real elapsed time even if Timer.periodic drifts under load. The configured mTotalTime is not consumed as running state.

Implementation

void startCountDown() {
  if (_isActive || mInterval <= 0 || mTotalTime <= 0) return;
  _isActive = true;
  final DateTime endTime = now().add(Duration(milliseconds: mTotalTime));
  final Duration duration = Duration(milliseconds: mInterval);
  _doCallback(mTotalTime);
  _mTimer = Timer.periodic(duration, (Timer timer) {
    final int remaining = endTime.difference(now()).inMilliseconds;
    if (remaining >= mInterval) {
      _doCallback(remaining);
    } else if (remaining <= 0) {
      _doCallback(0);
      cancel();
    } else {
      // Final sub-interval: stop the periodic timer and schedule one
      // trailing tick at the precise end time.
      timer.cancel();
      _mTailTimer = Timer(Duration(milliseconds: remaining), () {
        if (!_isActive) return;
        _doCallback(0);
        cancel();
      });
    }
  });
}