How to use jQuery timeago to update in real time?

I have a value 011-04-29T14:55:33.000Z , this value falls into the jQuery template. I used timeago to convert the date to the past tense, but after it was written to the template, it does not have the ability to update with a lot of passes.

How can I implement something that will automatically update?

+4
source share
2 answers

Suppose you start with this (from the timeago homepage):

 <abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr> 

The timeago plugin will now change the title as it overwrites things. All you have to do is track the timestamp elsewhere, return it to the title attribute and rerun the plugin. Something like that:

 <abbr class="timeago" title="2008-07-17T09:24:17Z" data-ts="2008-07-17T09:24:17Z" >July 17, 2008</abbr> 

This will become the following:

 <abbr class="timeago" title="July 17, 2008" data-ts="2008-07-17T09:24:17Z" >2 years ago</abbr> 

And if you want to update it, just return data-ts to title and restart the plugin:

 $('.timeago').each(function() { var $this = $(this); $this.attr('title', $this.data('ts')); }).timeago(); 

If you are using old jQuery, you may need $this.attr('data-ts') instead of $this.data('ts') .

+6
source

I tried the above, no luck. And I found it. may be helpful.

https://mattbradley.imtqy.com/livestampjs/

Here <span data-livestamp="your time goes here..."></span> enough.

Remember to add jquery.js and moment.js to livestamp.js

+1
source

All Articles