I am attempting to showcase messages from a list for a duration of 1 second each. I cannot utilize the interval(1000)
method because I need to have the ability to clear a message as soon as a condition is met and then display the subsequent one for 1 second.
My current progress includes:
nextMessage() : Observable<any>{
this.currentMessage.next(this.messages[this.messagePointer++]);
return this.currentMessages;
}
subscribeMessage() : void{
this.currentMessage.pipe(switchMap(_=timer(1000).pipe(map(x=>this.nextMessage())))).subscribe(x=>console.log(x));
}
As it stands, this does not produce any results. My idea was that every second, the nextMessage
function would update the currentMessage
Observable, and if it was changed externally in between, the Timer would be reset. The subscribe method currently does not output anything, but when I try this instead:
this.currentMessage.pipe(tap(x=>console.log(x)),switchMap(_=timer(1000).pipe(map(x=>this.nextMessage())))).subscribe(x=>console.log(x));
I observe data values being displayed at 1-second intervals with the tap function working. Why does tap work but not the subscribe function?
EDIT
For a demonstration, you can view a StackBlitz Demo here