Sirop4ik
Sirop4ik

Reputation: 5263

Equivalent to debounce operator Rx

Actually I need to implement something like debounce operator, but let's take an example. If I set debounce for 5 sec, so user can click on button 10 time within 5 sec and just after 5 sec expire the last click event will be taken to account.

What I need is option when 5 sec set as a time range and when user click 10 times just first click will be taken to account immediately and all other clicks that were emitted within 5 sec time range will be dismissed.

After 5 sec user can start clicking again.

How to do it?

Upvotes: 0

Views: 98

Answers (1)

Jasdeep Singh
Jasdeep Singh

Reputation: 8321

There is another operator in RxJS to accomplish your task, throttleTime.

It will subscribe to the first value emitted and then wait for 5second and then again emit value after 5 second.

const source = fromEvent(document, 'click').pipe(
  throttleTime(5000),
);

Please find working example here: https://stackblitz.com/edit/rxjs-hwviuc

You can study more on throttleTime here

Upvotes: 2

Related Questions