https://github.com/krasimir/debounce
Dart package for exactly what you are thinking about - debouncing a function call.
https://github.com/krasimir/debounce
Last synced: 4 months ago
JSON representation
Dart package for exactly what you are thinking about - debouncing a function call.
- Host: GitHub
- URL: https://github.com/krasimir/debounce
- Owner: krasimir
- License: mit
- Created: 2020-12-06T10:02:09.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-06T17:51:42.000Z (almost 5 years ago)
- Last Synced: 2025-03-25T16:56:06.443Z (7 months ago)
- Language: Dart
- Size: 6.84 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# debounce
Dart package for exactly what you are thinking about - debouncing a function call.
```dart
void main() async {
Debouncer d = Debouncer(time: Duration(milliseconds: 100));Function func = () {
value += 1;
};d.debounce(func);
d.debounce(func);
d.debounce(func);
d.debounce(func);await Future.delayed(Duration(milliseconds: 110));
print(value); // 1
}
```The `Debouncer` class has an optional named parameter called `leading`. If you set it to `true` your callback will be fired immediately. For example:
```dart
void main() async {
Debouncer d = Debouncer(time: Duration(milliseconds: 100, leading: true));Function func = () {
value += 1;
};d.debounce(func); // at this point value is 1
d.debounce(func); // nothing happens
d.debounce(func); // nothing happens
d.debounce(func); // nothing happens
}
```---
This library exists to satisfy the need of debouncing callbacks passed to methods like `onPanUpdate`. For example:
```dart
Debouncer d = Debouncer(time: Duration(milliseconds: 300));
...
GestureDetector(
onPanUpdate: (details) {
d.debounce(() {
if (details.delta.dx > 0) {
print('left');
} else {
print('right');
}
});
},
```