debounce function

Function debounce(
  1. Function func, [
  2. int delay = 500
])

Implementation

Function debounce(Function func, [int delay = 500]) {
  Timer? timer;
  return ([Object? arg]) {
    if (timer?.isActive ?? false) {
      timer!.cancel(); // 取消之前的定时器
    }
    timer = Timer(Duration(milliseconds: delay), () {
      Function.apply(func, arg == null ? null : [arg]);
      timer = null;
    }); // 设置新的定时器
  };
}