throttleWithArgs<T> static method
节流函数(带参数)
限制函数执行频率,在指定时间内只执行一次
func 要节流的函数
delay 节流时间
返回节流后的函数和取消函数
Implementation
static (void Function(T), void Function()) throttleWithArgs<T>(
void Function(T) func,
Duration delay,
) {
bool isThrottled = false;
Timer? timer;
void throttledFn(T arg) {
if (!isThrottled) {
func(arg);
isThrottled = true;
timer = Timer(delay, () {
isThrottled = false;
timer = null;
});
}
}
void cancel() {
timer?.cancel();
timer = null;
isThrottled = false;
}
return (throttledFn, cancel);
}