5
votes

In RxJS, filters such as auditTime and throttleTime emit an Observable (in different ways) after a certain duration has passed. I need to emit an Observable and then wait for a period of time before emitting the next value.

In my case I am working in Angular. For example, this code:

this.fooService$.pipe(throttleTime(10000)).subscribe(() => this.doSomething());

will not accomplish what I need because the emission happens at the end of the duration. I need the opposite: the emission happens and then a delay. How can I accomplish this?

2
The leading and trailing options might be what you are looking for: github.com/ReactiveX/rxjs/blob/6.3.3/spec/operators/…cartant
try throttle()CruelEngine
@cartant You're suggestion is perfect. If you write it as answer I'll mark it as correct. Thanks.ebakunin
@ebakunin If you were able to use the suggestion to solve your problem, maybe you could write a self answer? I don't have time to write one up ATM.cartant

2 Answers

1
votes

What about using the classic timeout-function?

this.fooService$.subscribe( () =>
    setTimeout(() => { this.doSomething(); }, 3000)
);

EDIT:

Referring to your comment, do the following on your emitting side (just an example):

// RxJS v6+
import { timer, BehaviorSubject } from 'rxjs';

// timer starts with a delay of 1000 ms and then emits every 2000 ms
const source = timer(1000, 2000);
const emitter: BehaviorSubject<number> = new BehaviorSubject(-1);

const subscribe = source.subscribe( value => this.emitter.next(value));
0
votes

This should work and do operation in tap()

this.fooService$.pipe(
tap(() => this.doSomething()),
switchMap(()=>timer(10000))
)
.subscribe();