Drenai
Drenai

Reputation: 12347

Rxjs throttleTime - update the throttling in realtime?

Is there a way to update the throttleTime in Rxjs. You're scrolling near the bottom of the page, so we only need throttle every 400ms, while as you get nearer the top, we increate to 100ms as the target area is in that region

this.scrollSubscription$ = this.scrollSubject$
                .asObservable()
                .throttleTime(200, undefined, throttleTimeConfig)
                .subscribe(event => this.throttledScrollHandler(event));

What I am going to do is have two separate subscripbtions, as I cannot find a way of doing it.

this.slowerSubscriber$ = this.scrollSubject$
                    .asObservable()
                    .throttleTime(400,.......

Thought I would ask as I can't think of another way of achieving it

Upvotes: 2

Views: 538

Answers (1)

Ingo Bürk
Ingo Bürk

Reputation: 20033

You can use throttle to implement your own silencing duration:

// Make this function extract the position using scrollY, page offsets or whatever you want to use
const getScrollPositionFromEvent = event => 42;
this.scrollSubject$.asObservable()
  .throttle(event => Observable.interval(
    getScrollPositionFromEvent(event) <= 100 ? 100 : 400
  ))
  .subscribe(console.log);

You can see a working example here. It simply emits by a given input, which in your case would be based on the scroll offset.

Upvotes: 2

Related Questions