throttle static method

void throttle(
  1. String key,
  2. dynamic callback(), {
  3. Duration duration = const Duration(milliseconds: 500),
})

Throttle function that ensures the callback function is not called more often than the specified duration.

This function is useful when you want to limit the rate at which an operation is performed, for example, when listening to scroll events or updating the UI.

callback: The function to be executed with a throttled rate. duration: The minimum duration between consecutive callback executions.

Implementation

static void throttle(String key, Function() callback, {Duration duration = const Duration(milliseconds: 500)}) {
  if (_throttleTimers[key] == null) {
    _throttleTimers[key] = Timer(duration, () {
      callback();
      _throttleTimers.remove(key);
    });
  }
}