i’d like to periodically update the time on comments that get posted on my website. The format I’m interested in as Facebook and others have done where the time of the post is listed in relation to the current time. For example, the post should say, “just now…” when first submitted but then after 1 minute it should say “1 minute ago”, then “2 minutes ago” and so on.
I found this nice script to do this: http://forrst.com/posts/Facebook_style_live_dates_in_JavaScript-hro
and have gotten myself maybe half way there.
There are two issues with my code so far:
- the time that is being reported is screwed up.
- the time is not being updated every second even though the
setinterval()is working fine.
here is a snippet of the code which I put in its entirety in a jsfiddle: http://jsfiddle.net/Y8Q7p/16/. I think the issue is with var time.
$(document.body).on('click', 'button', function(){
var id= $(this).data('id'),
comment=$('textarea[data-id="'+id+'"]').val(),
start_timer = setInterval(function() {
var time = new Date();
time = time_since(time);
$('div[data-id="'+id+'"]').html(time);
},
1000);
$('#'+id).html(comment);
});
I’ve tested it, and this works: