throttle static method

VoidCallback throttle(
  1. VoidCallback func, {
  2. Duration delay = const Duration(milliseconds: 300),
})

Throttle function for limiting call frequency

Ensures function is called at most once per specified duration. Useful for scroll events, button clicks, etc.

Example:

final throttledScroll = FSUtils.throttle(() => handleScroll());
scrollController.addListener(throttledScroll);

Implementation

static VoidCallback throttle(
  VoidCallback func, {
  Duration delay = const Duration(milliseconds: 300),
}) {
  Timer? timer;
  return () {
    if (timer == null) {
      func();
      timer = Timer(delay, () => timer = null);
    }
  };
}