Running a Timer

Let's implement the interval timer for the stopwatch application using observables.

Interval timer

Let’s tackle the interval timer behind the scenes that maintains the state.

This timer will need to track the total number of seconds elapsed and emit the latest value every 1/10th of a second. When the stop button is pressed, the interval should be canceled. We’ll need an observable for this, leading to the question: “How on earth do I build an observable?”

Run the code below and see what happens? Nothing appears in the console; the constructor never runs. Don’t worry about knowing everything that’s going on, but take a few guesses as you go through it. We will see later in this lesson, why that is so.

Get hands-on with 1400+ tech skills courses.