Creating a stopwatch with redux : <p>I've been trying to make a stopwatch in react and redux. I've been having trouble trouble figuring out how to design such a thing in redux. </p>

<p>The first thing that came to mind was having a <code>START_TIMER</code> action which would set the initial <code>offset</code> value. Right after that, I use <code>setInterval</code> to fire off a <code>TICK</code> action over and over again that calculates how much time has passed by using the offset, adds it to the current time, and then updates the <code>offset</code>.</p>

<p>This approach seems to work, but I'm not sure how I would clear the interval to stop it. Also, it seems like this design is poor and there is probably a better way to do it.</p>

<p>Here is a full <a href="https://jsfiddle.net/a94jabzd/1/" rel="noreferrer">JSFiddle</a> that has the <code>START_TIMER</code> functionality working. If you just want to see what my reducer looks like right now, here it is:</p>

<pre><code>const initialState = {
  isOn: false,
  time: 0
};

const timer = (state = initialState, action) =&gt; {
  switch (action.type) {
    case 'START_TIMER':
      return {
        ...state,
        isOn: true,
        offset: action.offset
      };

    case 'STOP_TIMER':
      return {
        ...state,
        isOn: false
      };

    case 'TICK':
      return {
        ...state,
        time: state.time + (action.time - state.offset),
        offset: action.time
      };

    default: 
      return state;
  }
}
</code></pre>

<p>I would really appreciate any help.</p>
