mirror of
https://github.com/wgh136/pixes.git
synced 2026-03-27 05:33:16 +08: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;
|
|
}
|
|
} |