executeOnce static method

void executeOnce(
  1. void callback(), {
  2. Duration duration = const Duration(milliseconds: 500),
})

Executes the specified callback immediately, and then debounces subsequent calls to the same function until the specified duration has elapsed.

If a debounced call is made before the duration has elapsed, the timer is reset and the callback is not executed until the timer has elapsed again.

Implementation

static void executeOnce(void Function() callback, {Duration duration = const Duration(milliseconds: 500)}) {
  if (_timer?.isActive ?? false) {
    _timer!.cancel();
  } else {
    callback();
  }
  _timer = Timer(duration, () {
    _timer = null;
  });
}