debounce static method

Function debounce(
  1. Function func, {
  2. String? id,
  3. int timeout = _defaultDuration,
})

防抖

Implementation

static Function debounce(
  Function func, {
  String? id,
  int timeout = _defaultDuration,
}) {
  return () {
    final String key = id ?? func.hashCode.toString();
    Timer? timer = _funcDebounce[key];
    if (timer == null) {
      func.call();
      timer = Timer(Duration(milliseconds: timeout), () {
        Timer? t = _funcDebounce.remove(key);
        t?.cancel();
        t = null;
      });
      _funcDebounce[key] = timer;
    }
  };
}