throttle static method

Function throttle(
  1. dynamic func(), {
  2. String? id,
  3. int timeout = _defaultDuration,
  4. Function? funcWhenComplete,
})

节流

Implementation

static Function throttle(
  Function() func, {
  String? id,
  int timeout = _defaultDuration,
  Function? funcWhenComplete,
}) {
  return () {
    final String key = id ?? func.hashCode.toString();
    final bool enable = _funcThrottle[key] ?? true;
    if (enable) {
      _funcThrottle[key] = false;
      Timer(Duration(milliseconds: timeout), () {
        _funcThrottle.remove(key);
      });
      func.call();
    }
  };
}