delayCancellable method

Timer delayCancellable(
  1. VoidCallback callback
)

Utility to delay some callback (or code execution) with the ability to cancel. Returns a Timer that can be cancelled using timer.cancel().

Sample:

void main() {
  print('+ start delay');
  final timer = 5.delayCancellable(() => print('- callback called'));
  print('- cancelling after 2 seconds');
  2.delay().then((_) {
    timer.cancel();
    print('- timer cancelled');
  });
}

Implementation

Timer delayCancellable(VoidCallback callback) =>
    Timer(Duration(milliseconds: (this * 1000).round()), callback);