Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Momentjs doesn't update time .fromNow()

Tags:

meteor

When I load the page all is working fine, but: why momentjs doesn't update the time?
If I insert a new post is inserted, momentjs doesn't update the a few seconds ago text with a minute ago.

This is what I'm doing.

I've added the momentjs:moment package

Registered the helper

UI.registerHelper('timeAgo', function(datetime) {
    return moment(datetime).fromNow();
});

In the Template

<template name="postItem">
    <li>
      {{text}} - {{timeAgo createdAt}}
    </li>
  {{/if}}
</template>

The output is

Hi! - a few seconds ago
Hello! - 23 minutes ago    
like image 563
Massimiliano Marini Avatar asked Feb 11 '23 06:02

Massimiliano Marini


1 Answers

The date isn't a reactive variable so it won't refresh when the time difference changes.

You can make it reactive by forcing it to recalculate every minute (or a custom interval):

UI.registerHelper('timeAgo', function(datetime) {
    Session.get('time');
    return moment(datetime).fromNow();
});

setInterval(function() {
    Session.set("time", new Date())
}, 60000); //Every minute

This will force the helper to redraw the value for time every minute.

like image 86
Tarang Avatar answered Mar 20 '23 00:03

Tarang