debounce static method
函数防抖
Implementation
static void Function()? debounce(Function? fn, [int delay = 1000, immediate = true]) {
Timer? timer;
return () {
// 还在时间之内,抛弃上一次
if (timer?.isActive ?? false) timer?.cancel();
if (immediate) {
bool callNow = (timer == null);
timer = Timer(Duration(milliseconds: delay), () {
timer?.cancel();
timer = null;
});
if (callNow) fn!();
} else {
timer = Timer(Duration(milliseconds: delay), () {
fn!();
});
}
};
}