sampleTime() operator

The sampleTime() operator is used to only emit values after the sample period has passed. A good use case for this is when you want to have a cooldown functionality. Imagine you have users that press a Save button way too often. It might be that saving takes a few seconds to complete. A way to approach this is to disable the Save button while saving. Another valid approach is to simply ignore any presses of the button until the operation has had the chance to complete. The following code does just that:

// time/sampleTime.jslet elem = document.getElementById("btn");let stream$ = Rx.Observable  .fromEvent(elem, "click")  .sampleTime(8000);// emits values every 8th secondstream$.subscribe(data => console.log("mouse clicks" ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.