D
D
Drovosek012019-07-02 16:09:01
Angular
Drovosek01, 2019-07-02 16:09:01

How to take value from infinite stream during click in rxjs?

Hello.
There is 1 Observable - a click in the document and an interval. The interval starts working from the moment the page is loaded. Can you please tell me how to more elegantly take the value from the interval during the click?
So far it works like this, but I'm confused by the "extra" intermediary variable currentTime.

export class AppComponent implements OnInit {
  private clickEvent$: Observable = fromEvent(document, 'click');
  private timeSec$: Observable = interval(1000);

  private currentTime: number;
  private clickedTime: number;

  ngOnInit() {
    this.clickEvent$.subscribe(
      _ => {
        this.isClicked = true;
        this.clickedTime = this.currentTime;
      }
    );
    this.timeSec$.subscribe(intervalValue => this.currentTime = intervalValue);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-07-02
@Drovosek01

withLatestFrom
UPD. https://stackblitz.com/edit/angular-govuo1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question