2023-11-16 13:12:55 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
class DelayedFunctionCaller {
|
2024-01-08 11:56:35 +01:00
|
|
|
final void Function() function;
|
2023-11-16 13:12:55 +01:00
|
|
|
final Duration duration;
|
|
|
|
Timer? _timer;
|
|
|
|
|
|
|
|
DelayedFunctionCaller(this.function, this.duration);
|
|
|
|
|
2024-01-09 12:47:42 +01:00
|
|
|
get scheduled => _timer != null && _timer!.isActive;
|
|
|
|
|
2023-11-16 13:12:55 +01:00
|
|
|
void call() {
|
|
|
|
// If a timer is already active, return.
|
|
|
|
if (_timer != null && _timer!.isActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timer that calls the function after the specified duration.
|
2024-01-08 11:56:35 +01:00
|
|
|
_timer = Timer(duration, function);
|
2023-11-16 13:12:55 +01:00
|
|
|
}
|
|
|
|
}
|