[Vuejs]-How to start stopwatch from state time using Vue js

0👍

Looks like the issue is that the 10s is not added to the timeBegan. If you subtract the 10 seconds, it should work.

if (state.timeBegan === null) {
  state.timeBegan = new Date() - 10_000;
}

On a side note, a mutation should be limited to just the mutation

This code is adding and removing interval from within the mutation and goes against how mutations are supposed to work.

    start(state, payload) {
      state.started = setInterval(() => {
        payload.callback();
      }, 10);
      state.running = true;
    },
    stop(state) {
      state.running = false;
      state.timeStopped = new Date();
      clearInterval(state.started);
    },

And executing a callback from the mutation is also not good. Not that it won’t work, but it’s creating a weird state flow that goes against the principles of how the store works.

Leave a comment