41 lines
793 B
Dart
41 lines
793 B
Dart
/* 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;
|
|
target(T value) {
|
|
if (timer?.isActive ?? false) {
|
|
timer?.cancel();
|
|
}
|
|
timer = Timer(delay, () {
|
|
if (func != null) func.call(value);
|
|
});
|
|
}
|
|
|
|
return target;
|
|
}
|