Set Intervals with RxJS interval and timer Operators

André Staltz
InstructorAndré Staltz

Share this video with your friends

Send Tweet

It is quite common to need an Observable that ticks periodically, for instance every second or every 100 miliseconds. We will learn about operators interval() and timer(), both of which are similar to setInterval() in JavaScript.

Sean Inglis
Sean Inglis
~ 3 years ago

It was unclear (magical even) where the index value "i" was initialized and maintained in the second version of the code. It continues to be delivered to the subscribers but makes no appearance in the code.

You say @ 1:30 or so that the new Observable has internal to it the machinery of remembering the increment, but how did it get there? It looks in the code as if you reasssigned foo, with no mention of those internals and yet it was transferred.

Can you explain what I'm missing here? Ta.

Jasmine Xie
Jasmine Xie
~ 2 years ago

i is a function-scoped variable that the inner function has access to through its variable scope chain (think closures). Variables defined by var has function scope, which is quite different from the block scope defined by let and const, or the variable scope of other programming languages like C and Java. Observables don't actively maintain it - Javascript does.