Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
559 B

1 year ago
  1. import 'dart:async';
  2. class Debounce {
  3. static final Map<String, Timer> _timers = {};
  4. static void debounce(String tag, Duration duration, Function onExecute) {
  5. if (duration == Duration.zero) {
  6. _timers[tag]?.cancel();
  7. _timers.remove(tag);
  8. onExecute();
  9. } else {
  10. _timers[tag]?.cancel();
  11. _timers[tag] = Timer(duration, () {
  12. _timers[tag]?.cancel();
  13. _timers.remove(tag);
  14. onExecute();
  15. });
  16. }
  17. }
  18. static void cancel(String tag) {
  19. _timers[tag]?.cancel();
  20. _timers.remove(tag);
  21. }
  22. }