Answer the question
In order to leave comments, you need to log in
How to change stream value after subscribe method?
I have an unusual task.
There is a timer, you need to add a button that will stop it for 300ms.
And all this needs to be implemented using rxJS.
I can’t figure out how to change the subscription value after clicking the button, I’ve probably already googled everything, I still can’t find niiieegggooooo.
If not difficult, I would like to see another live example of how this is done.
Answer the question
In order to leave comments, you need to log in
https://rxjs.dev/api/index/function/fromEvent
https://rxjs.dev/api/operators/concatMap
import { fromEvent, EMPTY, interval, merge, timer } from 'rxjs';
import { scan, startWith, switchMap, mapTo, delay } from 'rxjs/operators';
const DELAY=1000;
const button = document.createElement('button');
button.textContent = 'Click me';
document.documentElement.appendChild(button);
const click$ = fromEvent(button, 'click').pipe(
mapTo(false),
)
const unclick$ = click$.pipe(
delay(DELAY),
mapTo(true),
)
merge(click$, unclick$).pipe(
startWith(true),
switchMap(event => event ? timer(0, 1000) : EMPTY),
scan(acc => acc + 1, 0),
)
set to 300msmeans "to pause"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question