interval<T> method

void interval<T>(
  1. RxInterface<T> rx,
  2. void callback(
    1. T
    ), {
  3. Duration duration = const Duration(seconds: 1),
})
inherited

Calls callback at most once per duration, ignoring intermediate changes. Useful for rate-limiting UI updates.

interval(scrollPosition, (pos) => loadMore(pos),
  duration: const Duration(seconds: 1));

Implementation

void interval<T>(
  RxInterface<T> rx,
  void Function(T) callback, {
  Duration duration = const Duration(seconds: 1),
}) {
  bool canCall = true;
  _workers.add(rx.listen((val) {
    if (canCall) {
      canCall = false;
      callback(val);
      late Timer timer;
      timer = Timer(duration, () {
        _timers.remove(timer);
        canCall = true;
      });
      _timers.add(timer);
    }
  }));
}