debouncer.dart 452 B

1234567891011121314151617181920
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. class Debouncer {
  4. final int milliseconds;
  5. VoidCallback? action;
  6. Timer? _timer;
  7. Debouncer({required this.milliseconds});
  8. void run(VoidCallback action) {
  9. // 如果_timer不为空,我们取消之前的计时器
  10. if (_timer != null) {
  11. _timer!.cancel();
  12. }
  13. // 创建一个新的计时器
  14. _timer = Timer(Duration(milliseconds: milliseconds), action);
  15. }
  16. }