I'm using a stream which is throttled when I scroll the window.
While throttling (as long as scrolling), it emits values to the console.
However , when stream is idle (user is not scrolling the window) - I want a timer to kick in. However - if the user starts scrolling again - I don't want that timer to emit values.
Currently I'm doing this :
const observable = Rx.Observable.fromEvent(window, 'scroll');
const subscriber = observable
.throttleTime(300 )
.map(() => 'throttle')
.merge(Rx.Observable.interval(1000).map(() => 'tick') )
.subscribe(
(x) => {
console.log('Next: event!', x);
},
(err) => {
console.log('Error: %s', err);
},
() => {
console.log('Completed');
});
The problem is that , while scrolling - I see both "throttle"
AND "tick"
( I should only see "throttle")
Think of this from another POV. A job always has to run. If I scroll - that throttled scroll - should invoke the job. If I don't scroll - a timer should kick in and start doing the job . (and stops if user start scrolling again).
Question:
How can I start a timer after an idle time of not scrolling ?