RxDart - Using Delay, Timer, Periodic and Interval

This tutorial is about how to use delay, timer, periodic, and interval in RxDart.

If you are using RxDart and you need to delay item emission, there are some operators you can use. Below are examples of how to use Observable's delay, timer, periodic, interval, as well as using Future.delayed for delaying item emission.

Using delay

This instance method works by pausing for a certain amount of duration before starting to emit items from the source. The delay period is only applied once at the beginning, not for each item.

Example:

  Observable<int> o1 = Observable.fromIterable([1, 2, 3, 4]);

  print(new DateTime.now());
  o1.delay(new Duration(seconds: 2))
      .listen((value) {
        print('value: $value, time: ${new DateTime.now()}');
      });

Output:

  2019-12-23 15:24:55.271172
  value: 1, time: 2019-12-23 15:24:57.309985
  value: 2, time: 2019-12-23 15:24:59.315229
  value: 3, time: 2019-12-23 15:25:01.316044beginning
  value: 4, time: 2019-12-23 15:25:03.316895

Using timer

This static method emits the given value after a specified duration.

Example:

  print(new DateTime.now());
  Observable.timer('one', new Duration(seconds: 2))
      .listen((value) {
        print('value: $value, time: ${new DateTime.now()}');
      });

Output:

  2019-12-23 15:20:52.716407
  value: one, time: 2019-12-23 15:20:54.745354

Using periodic

This is a static method for creating an Observable that repeatedly emits an event every certain interval. This can be useful if you need to repeat a task every certain period.

Example:

  print(new DateTime.now());
  new Observable.periodic(new Duration(seconds: 2), (i) => i).take(3)
      .listen((value) {
        print('value: $value, time: ${new DateTime.now()}');
      });

Output:

  2019-12-23 14:56:18.931563
  value: 0, time: 2019-12-23 14:56:20.959707
  value: 1, time: 2019-12-23 14:56:22.943205
  value: 2, time: 2019-12-23 14:56:24.943123

Using interval

If you have an Observable that emits multiple items and you want to emit each item after a given duration, you can use interval. In addition, you can use timeInterval which records the time interval between values.

Example:

  Observable<int> o1 = Observable.fromIterable([1, 2, 3, 4]);

  o1.interval(new Duration(seconds: 2))
      .timeInterval()
      .listen(print);

Output:

  TimeInterval{interval: 0:00:02.025343, value: 1}
  TimeInterval{interval: 0:00:02.005227, value: 2}
  TimeInterval{interval: 0:00:02.000827, value: 3}
  TimeInterval{interval: 0:00:02.000917, value: 4}

Using Future.delayed For Delaying Each Item

Another way to add delay between emitted items is using Future.delayed.

Example:

  Observable<int> o1 = Observable.fromIterable([1, 2, 3, 4]);

  print(new DateTime.now());
  o1.asyncMap((i) => new Future.delayed(new Duration(seconds: 2), () => i))
      .listen((value) {
        print('value: $value, time: ${new DateTime.now()}');
      });

Output:

  2019-12-23 15:59:16.114583
  value: 1, time: 2019-12-23 15:59:18.144777
  value: 2, time: 2019-12-23 15:59:20.150157
  value: 3, time: 2019-12-23 15:59:22.151119
  value: 4, time: 2019-12-23 15:59:24.152039