mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 12:57:24 +00:00
28 lines
444 B
Dart
28 lines
444 B
Dart
import 'dart:async';
|
|
import 'dart:ui';
|
|
|
|
class Debounce {
|
|
final Duration delay;
|
|
VoidCallback? _action;
|
|
Timer? _timer;
|
|
|
|
Debounce({required this.delay});
|
|
|
|
void call(VoidCallback action) {
|
|
_action = action;
|
|
_timer?.cancel();
|
|
_timer = Timer(delay, _execute);
|
|
}
|
|
|
|
void _execute() {
|
|
if (_action != null) {
|
|
_action!();
|
|
_action = null;
|
|
}
|
|
}
|
|
|
|
void cancel() {
|
|
_timer?.cancel();
|
|
_action = null;
|
|
}
|
|
} |