2024-03-18 13:23:58 +08:00
|
|
|
// throttle.dart
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
/// 函数节流
|
|
|
|
///
|
|
|
|
/// [func]: 要执行的方法
|
|
|
|
Function throttler(
|
|
|
|
Future Function(String text) func,
|
|
|
|
) {
|
|
|
|
bool enable = true;
|
2024-03-31 17:13:29 +08:00
|
|
|
target(String value) {
|
2024-03-18 13:23:58 +08:00
|
|
|
if (enable == true) {
|
|
|
|
enable = false;
|
|
|
|
func(value).then((_) {
|
|
|
|
enable = true;
|
|
|
|
});
|
|
|
|
}
|
2024-03-31 17:13:29 +08:00
|
|
|
}
|
|
|
|
|
2024-03-18 13:23:58 +08:00
|
|
|
return target;
|
|
|
|
}
|