debounce method

dynamic Function() debounce([
  1. Duration delay = const Duration(seconds: 1)
])

Function debounce When an event is triggered, the target operation is not executed immediately. Instead, a delayed time is given. If another event is triggered within that time range, the delay time is reset. The target operation is executed only after the delay time expires. For example, if the delay time is set to 1000ms: If no event is triggered within 1000ms, the target operation is executed. If another event is triggered within 1000ms, the delay time is reset, and the target operation is executed after another 1000ms delay.

Implementation

Function() debounce([Duration delay = const Duration(seconds: 1)]) {
  Timer? timer;
  return () {
    if (timer?.isActive ?? false) timer?.cancel();
    timer = Timer(delay, () => this.call());
  };
}