The delay operator delays the emission of the source observable items by a specified time. The following figure shows the marble diagram of this operator:
Figure 9.17: The delay operator
Its prototype is the following:
Observable.delay(self, duetime, scheduler=None)
Here, the duetime parameter is the delay to apply in milliseconds. The scheduler parameter can be used to specify a scheduler to use. If no scheduler is provided, then the timeout scheduler is used.
Here is an example of the delay operator:
numbers = Observable.just(1)
print("{}".format(datetime.datetime.now()))
numbers.delay(200).subscribe(
on_next = lambda i: print("on_next {}: {}"
.format(i, datetime.datetime.now())),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)
time.sleep(0.5)
In this example, the numbers operator emits just one item, and its emission is delayed by 200 milliseconds.
This example gives the following result: