throttle function

Function throttle(
  1. Function func, [
  2. dynamic delay = 1500,
  3. dynamic leading = true
])

Implementation

Function throttle(Function func, [delay = 1500, leading = true]) {
  Timer? timer;
  return ([Object? arg]) {
    if (timer == null) {
      timer = Timer(Duration(milliseconds: delay), () {
        if (!leading) Function.apply(func, arg == null ? null : [arg]);
        timer = null;
      });
      if (leading) Function.apply(func, arg == null ? null : [arg]);
    }
  };
}