Search code examples
javascriptjquerytimeago

timeago.js Not Working as Expected


I have a class that holds the time each article is created. I want the time to be realtime, which is what timeago.js does but my time just stays at about 1 minute ago even though it is actually 4 minutes ago. I have to refresh the page to get the time correctly display.

$(document).ready(function(){
   $('.post_time').each(function(){
      var tthis = $( this );
      var tt = tthis.html();
      tthis.html($.timeago(tt));
   });
});

Solution

  • You write the result of the timeago method as a string into the element (by using html method), instead of binding the method to the element itself and let it do its work:

    HTML

    <div class="post_time" title="2014-11-29T18:13:00Z">2014-11-29</div>
    

    JavaScript

    $(document).ready(function(){
       $('.post_time').timeago();
    });
    

    JSFiddle