Okay , im want to make something like facebook’s auto time increment for posts and notificaitons etc.
when the post arrived for example , 5 seconds ago.
How do i use jQuery to make it auto increase without having to parse server for timeago.
It can increase till hours and days.
I have found a script , and tried modifying it , but its not working:
Any jquery plugin which automatic update time for all the posts of a page
$.fn.UpdateSince = function(interval) {
var times = this.map(function(){ return { e: $(this), t: parseInt($(this).html()) }; });
var format = function(t) {
if (t > 60) {
return Math.floor(t / 60) + ' minutes ago'
} else if(t > 3600){
return Math.floor(t / 3600) + ' hours ago'
} else if(t > 86400){
return Math.floor(t / 86400) + ' days ago'
} else {
return t + ' seconds ago';
}
}
var update = function(){
$.each(times, function(i, o){
o.e.html(format(Math.round((o.t + 1000))));
});
};
window.setInterval(update, interval);
update();
return this;
}
$('.TimeSince').UpdateSince(1000);
It looks like you removed the
nowvariable from the original script, which needs to know what time it is in order to run!The
getTime()method returns a value from your system clock – not the server.Incidentally, you should reverse the order of the conditional in your code – put “days” first, then “hours”, then “minutes”:
Otherwise, the function will always return “seconds” if
tis greater than60.