throttle method

void throttle(
  1. Duration duration,
  2. dynamic onThrottle(), {
  3. BehaviorType type = BehaviorType.leadingEdge,
})

Throttles the provided onThrottle function by executing it once, and preventing further invocations within the specified duration.

If the throttle method is called multiple times within the duration, only the first call will trigger the function. Subsequent calls during this period will be ignored until the duration has passed, at which point the function can be triggered again.

Note: The onThrottle function is executed immediately upon calling throttle if no previous call is active, otherwise, it will be delayed until the next eligible time slot after the duration.

Implementation

void throttle(
    Duration duration,
    Function() onThrottle, {
      BehaviorType type = BehaviorType.leadingEdge,
    }) {
  if (_throttleTimer == null) {
    _throttleTimer = Timer(duration, () {
      _throttleTimer = null;
      if (type == BehaviorType.trailingEdge || type == BehaviorType.leadingAndTrailing) {
        onThrottle();
      }
    });
    onThrottle();
  }
}