1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import 'dart:async';
- import 'package:flutter/widgets.dart';
- ValueChanged debounceValueChanged(ValueChanged fn, [int t = 500]) {
- Timer? _debounce;
- return (value) {
- // 还在时间之内,抛弃上一次
- if (_debounce?.isActive ?? false) _debounce?.cancel();
- _debounce = Timer(Duration(milliseconds: t), () {
- fn(value);
- });
- };
- }
- Function() debounce(
- Function func, [
- Duration delay = const Duration(milliseconds: 2000),
- ]) {
- Timer? timer;
- Function() target = () {
- if (timer?.isActive ?? false) {
- timer?.cancel();
- }
- timer = Timer(delay, () {
- func.call();
- });
- };
- return target;
- }
- Function() throttle(
- Future Function() func,
- ) {
- bool enable = true;
- Function() target = () {
- if (enable == true) {
- enable = false;
- func().then((_) {
- enable = true;
- });
- }
- };
- return target;
- }
|