【问题标题】:How to return an array from jQuery ajax success function and use it in a loop?如何从 jQuery ajax 成功函数返回一个数组并在循环中使用它?
【发布时间】:2012-03-20 12:54:27
【问题描述】:

我想让每个帖子的时间实时更改。

这只是带有时间的字体,因为这是要在这里很重要。

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

这是javascript

setInterval(updateTimestamps,30000);

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

function updateTimestamps(){

  $(".timestamp").each(function(i){

     var timestamp=$(this).attr("postdate"),
     postID=$(this).attr("postID"),
     math=realTime-timestamp;

  if(math<3600){var dataString='postID='+postID+'&timestamp='+timestamp;

           $.ajax({
                     type:"POST",
                     url:"http://site.com/ajax/humanTime.php",
                     data:dataString,
                     cache:false,
                     success:function(html){
                                $("[postID='"+postID+"']").html(html);
                     }

            });

  }

});

}

在 humanTime.php 我计算时间:

$timestamp = $_POST['timestamp'];

$now=time(); $diff= $now - $timestamp;  and so on..

但问题是它建立了许多连接,因为每个帖子都会调用脚本。并认为也许我可以建立 1 个连接,将数据排序到一个数组中,然后更改时间。但我从未使用过 json,我确定我想要的是否真的可能

【问题讨论】:

  • 您知道字体标签在 HTML4 中已被弃用并在 HTML5 中被删除,对吧?
  • 哇,90 年代已经过去,还有 ´`-tag!
  • 好的,但是用什么代替字体标签?
  • 由于 Math 是一个现有的 JavaScript 对象(和一个保留关键字),因此创建一个名为 math 的变量会令人困惑。以变​​量实际表示的名称命名变量,以使您的代码更易于重新阅读。

标签: java php javascript jquery json


【解决方案1】:

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

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';
    }
}

此函数借用自:http://www.queness.com/post/8567/create-a-dead-simple-twitter-feed-with-jquery

如前所述:使用&lt;span&gt; 标记或HTML5 标记&lt;time&gt;

【讨论】:

  • 在我的例子中,我还是使用了time()的php echo,这会导致一些不准确的地方。最好更改代码以使用 javascript 日期对象。
  • 但是,如果客户端机器没有正确设置时间怎么办?
  • 是的,这行得通,但我仍然需要使用服务器端脚本,因为我希望更改 currentTime。我不想仅仅更改为人类时间,而是每 20 秒更新一次,例如:facebook 每个帖子创建时间都在更新。
  • 在这种情况下,请查看上述链接中的代码,他们使用 javascript Date 对象而不是 php time(),如果您每次调用时都设置它,这将始终是最新的更新时间戳()
【解决方案2】:

我会这样做:

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");
}

编辑: 在humanTime.php中:

<?php 
    $ids = json_decode($_POST['time']);
    foreach($ids as $id) {
        // Calculate human time ...
    }
?>

然后返回如下内容:

[{id:1, content:1 minute ago}, {id:3, content: 5 hours ago}]

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

【讨论】:

  • 如何访问 humanTime.php 中的数据,因为我从未使用过 json
【解决方案3】:
<!-- if you like something simple :) -->
<span class="timestamp" data-unix="<?php echo time();?>">posted now</span>
<script type="text/javascript">
setInterval(function() {
     $('span').each(function() {
        var d = new Date();
        // difference in minutes from now
        var diff = Math.round(d.getTime()/60000 - $(this).attr('data-unix')/60);
        $(this).html(diff + ' mins ago');
    });
}, 60000);
</script>

【讨论】:

    【解决方案4】:

    如何在您的时间间隔在客户端处理它:

    示例标记可以是 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);
    

    工作示例:http://jsfiddle.net/MarkSchultheiss/6HKmS/

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 2015-10-09
      • 2011-01-12
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 2015-06-29
      相关资源
      最近更新 更多