How To Return An Array From Jquery Ajax Success Function And Use It In A Loop?
I want to make the time of each post to change in realTime. This is only the font with the time,beacuse this is want is the important here. ;
}
if (diff < minute) {
return Math.floor(diff / second) + 'seconds ago';
}
if (diff < minute * 2) {
return'about one minute';
}
if (diff < hour) {
return Math.floor(diff / minute) + 'minutes ago';
}
if (diff < hour * 2) {
return'about an hour ago';
}
if (diff < day) {
return Math.floor(diff / hour) + 'hours ago';
}
if (diff > day && diff < day * 2) {
return'yesterday';
}
if (diff < day * 365) {
return Math.floor(diff / day) + 'days ago';
}
else {
return'more then a year ago';
}
}
This function is borrowed from: http://www.queness.com/post/8567/create-a-dead-simple-twitter-feed-with-jquery
And as said: use a <span>
tag or the HTML5 tag <time>
.
Solution 2:
I would do something like this:
setInterval(updateTimestamps,30000);
var ids = newArray();
functionupdateTimestamps(){
$(".timestamp").each(function(i){
var obj = newObject();
obj.id = $(this).attr("postID");
obj.timestamp = $(this).attr("postdate");
ids.push(obj);
}
$.post("http://site.com/ajax/humanTime.php", {"time": ids}, function(data) {
for (i = 0; i < data.length; i++) {
$("#" + data[i].id).html(data[i].content);
}
}, "json");
}
EDIT: And in humanTime.php:
<?php$ids = json_decode($_POST['time']);
foreach($idsas$id) {
// Calculate human time ...
}
?>
then return something like this:
[{id:1, content:1 minute ago}, {id:3, content: 5 hours ago}]
PS: This solution is what you requested, but I think Bram aproach is better.
Solution 3:
<!-- if you like something simple :) --><spanclass="timestamp"data-unix="<?phpecho time();?>">posted now</span><scripttype="text/javascript">setInterval(function() {
$('span').each(function() {
var d = newDate();
// difference in minutes from nowvar diff = Math.round(d.getTime()/60000 - $(this).attr('data-unix')/60);
$(this).html(diff + ' mins ago');
});
}, 60000);
</script>
Solution 4:
What about processing it client side on your interval:
sample markup could be div, span etc.
<divclass="timestamp"postID="6"></div><divclass="timestamp"postID="2"></div><divclass="timestamp"postID="3"></div>
This supports present and future and generic.
var time_formats = [
[60, 'just now', 1],
[120, '1 minute ago', '1 minute from now'],
[3600, 'minutes', 60],
[7200, '1 hour ago', '1 hour from now'],
[86400, 'hours', 3600],
[172800, 'yesterday', 'tomorrow'],
[604800, 'days', 86400],
[1209600, 'last week', 'next week'],
[2419200, 'weeks', 604800],
[4838400, 'last month', 'next month'],
[29030400, 'months', 2419200],
[58060800, 'last year', 'next year'],
[2903040000, 'years', 29030400],
[5806080000, 'last century', 'next century'],
[58060800000, 'centuries', 2903040000]
];
functionprettydate(date_str) {
var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " ");
var seconds = (newDate() - newDate(time)) / 1000;
var token = 'ago';
var list_choice = 1;
if (seconds < 0) {
seconds = Math.abs(seconds);
token = 'from now';
list_choice = 2;
}
var i = 0,
format;
while (format = time_formats[i++]) if (seconds < format[0]) {
if (typeof format[2] == 'string') return format[list_choice];
elsereturnMath.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token;
}
return time;
}
// these would normally come from your ajax/creation of the element
$('.timestamp[postID=6]').data('postdate', '2012-03-20T09:24:17Z');
$('.timestamp[postID=2]').data('postdate', '2012-03-20T10:24:17Z');
$('.timestamp[postID=3]').data('postdate', '2012-03-20T08:24:17Z');
functionformatTimes() {
$('.timestamp').each(function() {
$(this).text(prettydate($(this).data('postdate')));
});
}
setTimeout(formatTimes, 30000);
Working example: http://jsfiddle.net/MarkSchultheiss/6HKmS/
Post a Comment for "How To Return An Array From Jquery Ajax Success Function And Use It In A Loop?"