N
N
Nikita2020-04-14 10:34:47
JavaScript
Nikita, 2020-04-14 10:34:47

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

2 answer(s)
D
Dmitry Belyaev, 2020-04-14
@bingo347

https://rxjs.dev/api/index/function/fromEvent
https://rxjs.dev/api/operators/concatMap

A
Anton Shvets, 2020-04-14
@Xuxicheta

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),
)

if i understood correctly
set to 300ms
means "to pause"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question