D
D
Daniele_Viotti2019-08-08 12:01:54
JavaScript
Daniele_Viotti, 2019-08-08 12:01:54

Vuex: Getters - real-time update. state - new Date()?

Hello!
Briefly: how to make the "new Date()" state getter update in real-time?
There is the following Vuex store:

state: {
  datetime: new Date(),
},

getters: {
   getDatetime: state => state.datetime,
}

I pull this variable in the template using getDatetime:
computed: {
   getDatetime() {return this.$store.getters.getDatetime;}
}

However, the time "freezes" and does not change until the page is reloaded... Please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-08
@Daniele_Viotti

state: {
  date: null,
  interval: null,
},
mutations: {
  start(state) {
    if (!state.interval) {
      state.interval = setInterval(() => this.commit('update'), 1000);
    }
  },
  update(state) {
    state.date = new Date();
  },
},

https://jsfiddle.net/uh5nsp1v/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question