complete_timer 1.0.0 copy "complete_timer: ^1.0.0" to clipboard
complete_timer: ^1.0.0 copied to clipboard

A dart timer that can be configured to fire once or repeatedly with ability start, stop, resume and cancel.

example/complete_timer_example.dart

import 'dart:async';

import 'package:complete_timer/complete_timer.dart';

void normalTimerExample() {
  print('start normal timer');

  final CompleteTimer normalTimer = CompleteTimer(
    // must a non-negative Duration.
    duration: Duration(seconds: 5),

    // If periodic sets true
    // The callback is invoked repeatedly with duration intervals until
    // canceled with the cancel function.
    // Defaults to false.
    periodic: false,

    // If autoStart sets true timer starts automatically, default to true.
    autoStart: true,

    // The callback function is invoked after the given duration.
    callback: (timer) {
      print('normal example');
      timer.stop();
      // We call stop function before getting elapsed time to get a exact time.
      print('normal timer finished after: ${timer.elapsedTime}');
    },
  );

  // After 2 seconds we stop timer and 2 second later we resume it
  Future.delayed(Duration(seconds: 2), () {
    print('stopped normal timer');
    normalTimer.stop();
  });

  Future.delayed(Duration(seconds: 4), () {
    print('resumed normal timer');
    normalTimer.start();
  });
}

void periodic(List<String> args) {
  print('periodic timer started');

  // Creates a periodic timer which fires callback every seconds
  final CompleteTimer periodicTimer = CompleteTimer(
    // must a non-negative Duration.
    duration: Duration(seconds: 1),
    // If periodic sets true
    // The callback is invoked repeatedly with duration intervals until
    // canceled with the cancel function.
    // Defaults to false.
    periodic: true,
    // If autoStart sets true timer starts automatically, default to true.
    autoStart: true,
    callback: (timer) {
      print('periodic example');
      if (timer.tick == 5) {
        // We call stop function before getting elapsed time to get a exact time
        timer.stop();
        print('periodic timer finished after: ${timer.elapsedTime}');
        timer.cancel();
      }
    },
  );

  // After 2 seconds we stop timer and 2 second later we resume it
  Future.delayed(Duration(seconds: 2), () {
    print('stopped periodic timer');
    periodicTimer.stop();
  });

  Future.delayed(Duration(seconds: 4), () {
    print('resumed periodic timer');
    periodicTimer.start();
  });
}
18
likes
160
points
132
downloads

Publisher

unverified uploader

Weekly Downloads

A dart timer that can be configured to fire once or repeatedly with ability start, stop, resume and cancel.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on complete_timer