throttle static method
void
throttle(})
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);
});
}
}