Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/llucax/pausable_timer
A Dart timer that can be paused, resumed and reset.
https://github.com/llucax/pausable_timer
dart dartlang timer
Last synced: 2 months ago
JSON representation
A Dart timer that can be paused, resumed and reset.
- Host: GitHub
- URL: https://github.com/llucax/pausable_timer
- Owner: llucax
- License: bsd-3-clause
- Created: 2020-10-06T14:17:12.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-22T14:40:50.000Z (3 months ago)
- Last Synced: 2024-12-07T07:41:10.947Z (3 months ago)
- Topics: dart, dartlang, timer
- Language: Dart
- Homepage: https://pub.dev/packages/pausable_timer
- Size: 149 KB
- Stars: 30
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# pausable\_timer
[](https://github.com/llucax/pausable_timer/actions?query=branch%3Amain+workflow%3ACI+)
[](https://github.com/llucax/pausable_timer/actions?query=branch%3Amain+workflow%3A%22Pub+Score%22+)
[](https://github.com/llucax/pausable_timer/actions/workflows/check-dart.yaml)
[](https://codecov.io/gh/llucax/pausable_timer)
[](https://pub.dev/packages/pausable_timer)
[](https://pub.dev/packages/pausable_timer/score)
[](https://pub.dev/packages/pausable_timer/score)
[](https://pub.dev/packages/pausable_timer/score)
[-555555?style=flat-square)](https://github.com/llucax/llucax/blob/main/sponsoring-platforms.md)[](https://github.com/sponsors/llucax)[](https://liberapay.com/llucax/donate)[](https://www.paypal.com/donate?hosted_button_id=UZRR3REUC4SY2)[](https://www.buymeacoffee.com/llucax)[](https://www.patreon.com/llucax)[](https://flattr.com/@llucax)
[-555555?style=flat-square)](https://github.com/sponsors/mateusfccp)[](https://github.com/sponsors/mateusfccp)A [Dart](https://dart.dev/)
[timer](https://api.dart.dev/stable/dart-async/Timer/Timer.html) that can be
paused, resumed and reset.## Example using `start()`, `pause()` and `reset()`
```dart
import 'package:pausable_timer/pausable_timer.dart';void main() async {
print('Create a timer that fires in 1 second, but it is not started yet');
final timer = PausableTimer(Duration(seconds: 1), () => print('Fired!'));
print('So we start it');
timer.start();print('And wait 1/2 second...');
await Future.delayed(timer.duration ~/ 2);
print('Not yet fired, still 1/2 second to go!');print('We can pause it now');
timer.pause();// When paused, time can pass but the timer won't be fired
print('And we wait a whole second...');
await Future.delayed(timer.duration);
print("But our timer doesn't care while it's paused");
print('It will still wait for ${timer.duration - timer.elapsed} after '
"it's started again");print('So we start it again');
timer.start();
print('And wait for 1/2 second again, it should have fired when we are done');
await Future.delayed(timer.duration ~/ 2);
print('And we are done, "Fired!" should be up there 👆');
print('Now our timer completed ${timer.tick} tick');print('We can reset it if we want to use it again');
timer.reset();
print('We have to start it again after the reset because it was not running');
timer.start();
print('Now we wait a whole second in one go...');
await Future.delayed(timer.duration);
print('And we are done, so you should see "Fired!" up there again 👆');
print('Now the timer has ${timer.tick} ticks');print('We can reset it and start it again');
timer.reset();
timer.start();print('And you can cancel it too, so it will not fire again');
timer.cancel();
print("After a timer is cancelled, it can't be used again");
print('But important information can still be retrieved:');
print('duration: ${timer.duration}');
print('elapsed: ${timer.elapsed}');
print('tick: ${timer.tick}');
print('isPaused: ${timer.isPaused}');
print('isActive: ${timer.isActive}');
print('isExpired: ${timer.isExpired}');
print('isCancelled: ${timer.isCancelled}');
}
```## Example pausable countdown implementation
```dart
// Example on how to implement countdown using PausableTimer.periodic
import 'dart:async';import 'package:pausable_timer/pausable_timer.dart';
void main() async {
// We make it "late" to be able to use the timer in the timer's callback.
late final PausableTimer timer;
var countDown = 5;print('Create a periodic timer that fires every 1 second and starts it');
timer = PausableTimer.periodic(
Duration(seconds: 1),
() {
countDown--;if (countDown == 0) {
timer.pause();
}print('\t$countDown');
},
)..start();print('And wait 2.1 seconds...');
print('(0.1 extra to make sure there is no race between the timer and the '
'waiting here)');
await Future.delayed(timer.duration * 2.1);
print('By now 2 events should have fired: 4, 3\n');print('We can pause it now');
timer.pause();print('And we wait for 2 more seconds...');
await Future.delayed(timer.duration * 2);
print("But our timer doesn't care while it's paused\n");print('So we start it again');
timer.start();
print('And wait for 3.1 seconds more...');
await Future.delayed(timer.duration * 3.1);
print('And we are done: 2, 1 and 0 should have been printed');print('The timer should be unpaused, inactive, expired and not cancelled');
print('isPaused: ${timer.isPaused}');
print('isActive: ${timer.isActive}');
print('isExpired: ${timer.isExpired}');
print('isCancelled: ${timer.isCancelled}');print('We can now reset it and start it again, now for 3 seconds');
countDown = 3;
timer
..reset()
..start();
print('And wait for 3.1 seconds...');
await Future.delayed(timer.duration * 3.1);
print('And it should be done printing: 2, 1 and 0');
}
```