I am trying to create a blog site and use time ago to create a relative time stamp to when the a post is created.
我正在尝试创建一个博客站点并使用时间以创建一个相对时间戳到创建一个帖子。
I have it working but the time does not update automatically or at all for a fact
我有它的工作,但时间不会自动更新或根本没有更新
Does anyone know how to get it to update automatically
有谁知道如何让它自动更新
Here is my code:
这是我的代码:
msgDate = jQuery.timeago(new Date());
$("#centerDiv").prepend("<div id='postInner'><div id='timeStamp'>" + msgDate + "</div></div>")
2 个解决方案
#1
2
timeago doesn't automagically set a timer to update your HTML.
You'll have to use setInterval
to create a timer that will update the time stamp.
timeago不会自动设置计时器来更新您的HTML。您必须使用setInterval创建一个更新时间戳的计时器。
Example:
例:
// Insert your HTML:
$("#centerDiv").prepend("<div id='postInner'><div id='timeStamp'></div></div>")
// Get the timestamp:
var msgDate = new Date();
// Update now, and again every minute:
updateTimeStamp();
setInterval(updateTimeStamp, 60000);
function updateTimeStamp() {
// Update the text with the elapsed time:
var timeAgo = jQuery.timeago(msgDate);
$("#timeStamp").text(timeAgo);
}
#2
1
May be timeago.js is more simple and tiny.
可能是timeago.js更简单,更小。
npm install timeago.js
npm install timeago.js
import it
导入它
<script src="dist/timeago.min.js"></script>
Then code below, can render realtime.
然后下面的代码可以实时渲染。
<time class="need_to_be_rendered" datetime="2016-06-30 09:20:00"></time>
// use render to render it realtime
timeago().render(document.querySelectorAll('.need_to_be_rendered'));
Will real-time render.
将实时渲染。
#1
2
timeago doesn't automagically set a timer to update your HTML.
You'll have to use setInterval
to create a timer that will update the time stamp.
timeago不会自动设置计时器来更新您的HTML。您必须使用setInterval创建一个更新时间戳的计时器。
Example:
例:
// Insert your HTML:
$("#centerDiv").prepend("<div id='postInner'><div id='timeStamp'></div></div>")
// Get the timestamp:
var msgDate = new Date();
// Update now, and again every minute:
updateTimeStamp();
setInterval(updateTimeStamp, 60000);
function updateTimeStamp() {
// Update the text with the elapsed time:
var timeAgo = jQuery.timeago(msgDate);
$("#timeStamp").text(timeAgo);
}
#2
1
May be timeago.js is more simple and tiny.
可能是timeago.js更简单,更小。
npm install timeago.js
npm install timeago.js
import it
导入它
<script src="dist/timeago.min.js"></script>
Then code below, can render realtime.
然后下面的代码可以实时渲染。
<time class="need_to_be_rendered" datetime="2016-06-30 09:20:00"></time>
// use render to render it realtime
timeago().render(document.querySelectorAll('.need_to_be_rendered'));
Will real-time render.
将实时渲染。