debounce static method

void Function() debounce(
  1. Function fun,
  2. int wait, {
  3. Object? key,
})

对函数进行防抖处理,如果在指定时间内多次执行函数,那么会忽略掉它,并重置等待时间,当等待时间结束后再执行函数

  • wait 防抖时间(毫秒)
  • key 指定函数标识符

Implementation

static void Function() debounce(Function fun, int wait, {Object? key}) {
  assert(wait > 0);
  key ??= fun.hashCode;
  return () {
    if (_debounceTimerMap.containsKey(key)) {
      _debounceTimerMap[key!]!.cancel();
      _debounceTimerMap.remove(key);
    }
    _debounceTimerMap[key!] = setTimeout(() {
      fun();
      _debounceTimerMap.remove(key);
    }, wait);
  };
}