Java 如何从jQueryAjax成功函数返回数组并在循环中使用它?

Java 如何从jQueryAjax成功函数返回数组并在循环中使用它?,java,php,javascript,jquery,json,Java,Php,Javascript,Jquery,Json,我想让每个帖子的时间实时变化 这只是时间的字体,因为这是最重要的 <font class="timestamp" postdate="unixTimeStamp" postID="6">2 min ago</font> <font class="timestamp" postdate="unixTimeStamp" postID="5">4 min ago</font> <font class="timestamp" postdate="

我想让每个帖子的时间实时变化

这只是时间的字体,因为这是最重要的

<font class="timestamp" postdate="unixTimeStamp" postID="6">2 min ago</font>

<font class="timestamp" postdate="unixTimeStamp" postID="5">4 min ago</font>

<font class="timestamp" postdate="unixTimeStamp" postID="4">9 min ago</font>

但问题是,它与许多连接有关,因为每个帖子都会调用脚本。我想也许我可以建立一个连接,将数据排序成一个数组,然后改变时间。但我从未使用过json,我确信如果我想要的真的是可能的,我会这样做:

setInterval(updateTimestamps,30000);
var ids = new Array();

function updateTimestamps(){
    $(".timestamp").each(function(i){
    var obj = new Object();
    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");
}
[{id:1, content:1 minute ago}, {id:3, content: 5 hours ago}]

PS:这个解决方案是您要求的,但我认为Bram aproach更好。

我会这样做:

setInterval(updateTimestamps,30000);
var ids = new Array();

function updateTimestamps(){
    $(".timestamp").each(function(i){
    var obj = new Object();
    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");
}
[{id:1, content:1 minute ago}, {id:3, content: 5 hours ago}]

PS:这个解决方案是您要求的,但我认为Bram aproach更好。

为什么要计算人工时间服务器端?您也可以在客户端完美地完成这些计算

setInterval(updateTimestamps,30000);

var currentTime=<?php echo time();?>;

function updateTimestamps(){

  $(".timestamp").each(function(i){
        var timestamp=$(this).attr("postdate");
        $(this).text(humanTime(currentTime, timestamp));
    });

}

function humanTime(currentTime, timestamp) 
{
    var diff = currentTime - timestamp,
    minute = 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;

    if (isNaN(diff) || diff < 0) {
        return ""; // return blank string if unknown
    }

    if (diff < second * 2) {
        // within 2 seconds
        return 'right now';
    }

    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';
    }
}
setInterval(updateTimestamps,30000);
var currentTime=;
函数updateTimestamps(){
$(“.timestamp”)。每个(函数(i){
var timestamp=$(this.attr(“postdate”);
$(this).text(humanTime(currentTime,timestamp));
});
}
函数humanTime(当前时间、时间戳)
{
var diff=当前时间-时间戳,
分钟=60,
小时=分钟*60,
天=小时*24,
周=天*7;
if(isNaN(diff)| | diff<0){
return“”;//如果未知,则返回空字符串
}
如果(差值<秒*2){
//2秒内
返回“立即”;
}
如果(差值<分钟){
返回数学地板(差/秒)+“秒前”;
}
如果(差值<分钟*2){
返回“大约一分钟”;
}
如果(差值<小时){
返回数学地板(差异/分钟)+“分钟前”;
}
如果(差值<小时*2){
返回“大约一小时前”;
}
如果(差异<天){
返回数学地板(差异/小时)+“小时前”;
}
如果(差异>天和差异<天*2){
返回“昨天”;
}
如果(差异<天*365){
返回数学地板(差异/天)+“几天前”;
}
否则{
返回“一年多以前”;
}
}
此功能借用自:


如前所述:使用
标记或HTML5标记

为什么要计算人工时间服务器端?您也可以在客户端完美地完成这些计算

setInterval(updateTimestamps,30000);

var currentTime=<?php echo time();?>;

function updateTimestamps(){

  $(".timestamp").each(function(i){
        var timestamp=$(this).attr("postdate");
        $(this).text(humanTime(currentTime, timestamp));
    });

}

function humanTime(currentTime, timestamp) 
{
    var diff = currentTime - timestamp,
    minute = 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;

    if (isNaN(diff) || diff < 0) {
        return ""; // return blank string if unknown
    }

    if (diff < second * 2) {
        // within 2 seconds
        return 'right now';
    }

    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';
    }
}
setInterval(updateTimestamps,30000);
var currentTime=;
函数updateTimestamps(){
$(“.timestamp”)。每个(函数(i){
var timestamp=$(this.attr(“postdate”);
$(this).text(humanTime(currentTime,timestamp));
});
}
函数humanTime(当前时间、时间戳)
{
var diff=当前时间-时间戳,
分钟=60,
小时=分钟*60,
天=小时*24,
周=天*7;
if(isNaN(diff)| | diff<0){
return“”;//如果未知,则返回空字符串
}
如果(差值<秒*2){
//2秒内
返回“立即”;
}
如果(差值<分钟){
返回数学地板(差/秒)+“秒前”;
}
如果(差值<分钟*2){
返回“大约一分钟”;
}
如果(差值<小时){
返回数学地板(差异/分钟)+“分钟前”;
}
如果(差值<小时*2){
返回“大约一小时前”;
}
如果(差异<天){
返回数学地板(差异/小时)+“小时前”;
}
如果(差异>天和差异<天*2){
返回“昨天”;
}
如果(差异<天*365){
返回数学地板(差异/天)+“几天前”;
}
否则{
返回“一年多以前”;
}
}
此功能借用自:

如前所述:使用
标签或HTML5标签




在您的时间间隔内在客户端处理it怎么样:

示例标记可以是div、span等

<div class="timestamp" postID="6"></div>
<div class="timestamp" postID="2"></div>
<div class="timestamp" postID="3"></div>

这支持当前、未来和通用

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]
    ];

function prettydate(date_str) {
    var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " ");
    var seconds = (new Date() - new Date(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];
        else return Math.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');

function formatTimes() {
    $('.timestamp').each(function() {
        $(this).text(prettydate($(this).data('postdate')));
    });
}
setTimeout(formatTimes, 30000);
var-time\u格式=[
[60,‘刚才’,1],
[120,‘1分钟前’、‘1分钟后’,
[3600,'分钟',60],
[7200,'一小时前','一小时后',
[86400,'小时',3600],
[172800,‘昨天’、‘明天’],
[604800,‘天’,86400],
[1209600,‘上周’、‘下周’],
[2419200,‘周’,604800],
[4838400,‘上个月’、‘下个月’],
[29030400,'月',2419200],
[58060800,‘去年’、‘明年’],
[2903040000,‘年’,29030400],
[5806080000,‘上世纪’、‘下世纪’],
[58060800000,‘世纪’,2903040000]
];
函数prettydate(date\u str){
变量时间=(“”+日期)。替换(/-/g,“/”)。替换(/[TZ]/g,“”);
var seconds=(新日期()-新日期(时间))/1000;
var标记='ago';
var列表_选项=1;
如果(秒<0){
秒=Math.abs(秒);
令牌=‘从现在起’;
列表选择=2;
}
var i=0,
格式;
while(格式=时间_格式[i++])if(秒<格式[0]){
if(typeof format[2]=“string”)返回格式[列表选择];
否则返回Math.floor(秒/格式[2])+“”+格式[1]+“”+令牌;
}
返回时间;
}
//这些通常来自ajax/元素的创建
$('.timestamp[postID=6]')。数据('postdate','2012-03-20T09:24:17Z');
$('.timestamp[postID=2]')。数据('postdate','2012-03-20T10:24:17Z');
$('.timestamp[postID=3]')。数据('postdate','2012-03-20T08:24:17Z');
函数formatTimes(){
$('.timestamp')。每个(函数(){
$(this).text(prettydate($(this).data('postdate'));
});
}
设置超时(格式化次数,30000);

工作示例:

在您的时间间隔内在客户端处理it怎么样:

示例标记可以是div、span等

<div class="timestamp" postID="6"></div>
<div class="timestamp" postID="2"></div>
<div class="timestamp" postID="3"></div>

这支持当前、未来和通用

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]
    ];

function prettydate(date_str) {
    var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " ");
    var seconds = (new Date() - new Date(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];
        else return Math.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');

function formatTimes() {
    $('.timestamp').each(function() {
        $(this).text(prettydate($(this).data('postdate')));
    });
}
setTimeout(formatTimes, 30000);
var-time\u格式=[
[60,‘刚才’,1],
[120,‘1分钟前’、‘1分钟后’,
[3600,'分钟',60],
[7200,'一小时前','一小时后',
[86400,'小时',3600],
[172800,‘昨天’、‘明天’],
[604800,‘天’,86400],
[1209600,‘上周’、‘下周’],
[2