mobile_skt/lib/util/debouncer.dart

40 lines
808 B
Dart
Raw Normal View History

/* import 'package:flutter/foundation.dart';
import 'dart:async';
class Debouncer {
final int milliseconds;
VoidCallback action;
Timer _timer;
Debouncer({ milliseconds });
/* */
run(VoidCallback action) {
if (_timer != null) {
_timer.cancel();
}
_timer = Timer(Duration(milliseconds: milliseconds), action);
}
}
*/
import 'dart:async';
/// 函数防抖
///
/// [func]: 要执行的方法
/// [delay]: 要迟延的时长
Function(T) debouncer<T>(Function(T text)? func, {int? delayTime}) {
Duration delay = Duration(milliseconds: delayTime ?? 1500);
Timer? timer;
Function(T) target = (T value) {
if (timer?.isActive ?? false) {
timer?.cancel();
}
timer = Timer(delay, () {
if (func != null) func.call(value);
});
};
return target;
}