Vue momentjs update relative time in real time

前端 未结 2 993
遥遥无期
遥遥无期 2021-01-11 17:19

How I can do update relative time from momentjs in real time for user?

I have computed:

computed: {
  ago() {
     return moment().fromNow         


        
相关标签:
2条回答
  • 2021-01-11 18:08

    Since moment().fromNow() isn't reactive so you will not see any change,to deal with we fix an old time property which should be initialized in the created hook this.oldTime = new Date();, and set a time interval with 1s, based on the old time property we call moment(this.old).fromNow(); to update our property ago.

    // ignore the following two lines, they just disable warnings in "Run code snippet"
    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    new Vue({
      el: '#app',
    
      data() {
        return {
          ago: '',
          oldTime: ''
        }
      },
    
      created() {
        this.oldTime = new Date();
        setInterval(() => {
          this.ago = moment(this.oldTime).fromNow();
        }, 1000)
      }
    });
    <link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
    
    <div id="app" class="container">
    
      <span class="text-muted pr-2" >
      {{ago}}
      </span>
    </div>

    Based on the comment of @Badgy :

    How would you handle it for a v-for where you show it in the UI via a function? I thought about attaching it to the message object on created and update all message objects every x seconds but not sure if its the best way

    to fit to this situation we should create a time interval in which we update the ago property of each message :

    // ignore the following two lines, they just disable warnings in "Run code snippet"
    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    new Vue({
      el: '#app',
    
      data() {
        return {
    
          messages: [{
              content: 'Hello !',
              time: '2019-09-10 00:08'
            },
            {
              content: 'Hello again!',
              time: '2019-09-10 00:10'
            }
          ]
    
        }
      },
      computed: {
        msgs() {
          return messages
    
        }
      },
      created() {
    
        setInterval(() => {
    
          this.messages = this.messages.map(m => {
            m.ago = moment(m.time).fromNow();
            return m;
          })
        }, 1000)
      }
    });
    .primary{
    color:blue
    }
    <link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
    <script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
    
    <div id="app" class="container">
    
      <ul>
        <li v-for="m in messages">{{m.content}} <span class="primary">{{m.ago}}</span></li>
      </ul>
    </div>

    0 讨论(0)
  • 2021-01-11 18:08

    I solve this using a global "ticker" in my Vuex store. It starts incrementing itself on load every 30 seconds. Descriptive messages of the time compare to this ticker instead of to other implementations of "now" or whatever. The advantage of this technique over something like every UI element iterating its own datetime ticker every second is that you don't have reactive elements in Vue all refreshing themselves on a regular basis all willy nilly. In my application, the one global "pseudo-now" ticker iterates every 30 seconds and all elements which provide descriptive accounts of the time all refresh at once in one frame. This benefits performance.

    snippet from a relevant Vuex store:

    import Vue from 'vue'
    
    const TICKER_INTERVAL = 30000; // milliseconds
    
    let tickerPointer = null;
    
    const data = {
      state: {
        now: new Date,
      },
    
      mutations: {
        refreshTicker(state) {
          state.now = new Date;
        },
      },
    
      actions: {
        startTicker({commit}) {
          if (tickerPointer === null) {
            tickerPointer = setInterval(() => {
              commit('refreshTicker');
            }, TICKER_INTERVAL);
          }
        },
    
        haltTicker({state}) {
          clearInterval(tickerPointer);
          state.now = null;
          tickerPointer = null;
        },
    

    "StartTicker" is dispatched once by the top of my application upon load.

    0 讨论(0)
提交回复
热议问题