javascript - 使用 javascript 定期更新发布评论的时间

标签 javascript jquery formatting setinterval

我想定期更新我网站上发布的评论的时间。我感兴趣的格式是 Facebook 和其他人所做的,其中列出了与当前时间相关的帖子时间。例如,帖子首次提交时应显示“刚刚……”,但 1 分钟后应显示“1 分钟前”,然后是“2 分钟前”,依此类推。

我发现这个很好的脚本可以做到这一点:http://forrst.com/posts/Facebook_style_live_dates_in_JavaScript-hro 或许我已经成功了一半。

到目前为止,我的代码存在两个问题:

  1. 所报告的时间被搞砸了。
  2. 即使 setinterval() 工作正常,时间也不会每秒更新。

这是我将其完整放入 jsfiddle 中的代码片段:http://jsfiddle.net/Y8Q7p/16/ 。我认为问题在于 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); 
});

最佳答案

我已经测试过了,this works :

$(document.body).on('click', 'button', function(){
    var id= $(this).data('id'),
    comment=$('textarea[data-id="'+id+'"]').val();
    var time =  new Date();
    start_timer = setInterval(function() {

        //var time =  new Date();
        var time2 = time_since(time.getTime()/1000);

        $('div[data-id="'+id+'"]').html(time2);

        },
        1000);
        $('#'+id).html(comment);
});


/**
 * date_suffix()
 * returns the date suffix (st,nd,rd,th) for a given day in a month
 *
 * @author: Andy Thomas (forrst@antom.co.uk)
 * @date: 27/09/2010
 */

function date_suffix(date) {
        if (date == 1 ||  date == 21 || date == 31) {
                return 'st';
        } else if (date == 2 || date == 22) {
                return 'nd';
        } else if (date == 3 || date == 23) {
                return 'rd';
        } else {
                return 'th';
        }
}

/**
 * time_since()
 * returns the time passed since a given unix_timestamp.
 * eg. 10 seconds ago, 1 hour ago, 10th Sep etc
 *
 * @author: Andy Thomas (forrst@antom.co.uk)
 * @date: 27/09/2010
 */
function time_since(original) {
        original = new Date(original * 1000);

        var str = '';

        var months = [
                'Jan',
                'Feb',
                'Mar',
                'Apr',
                'May',
                'Jun',
                'Jul',
                'Aug',
                'Sep',
                'Oct',
                'Nov',
                'Dec'
        ];

        var chunks = [
                [31536000000, 'year'],
                [2592000000, 'month'],
                [604800000, 'week'],
                [86400000, 'day'],
                [3600000, 'hour'],
                [60000, 'minute'],
                [1000, 'second'],
        ];

        var today = new Date();
        var since = new Date(today.getTime() - original.getTime());

        if (since.getTime() > 604800000) {
                str = months[original.getMonth()] + ' ' + original.getDate() + date_suffix(original.getDate());

                if (since.getTime() > 31536000000) {
                        str = str + ', ' + original.getFullYear();
                }

                return str;
        }

        var ms = 0;
        var name = 0;
        var i = 0;
        var ic = chunks.length;
        var count = 0;

        for (i=0;i<ic;i++) {
                ms = chunks[i][0];
                name = chunks[i][1];

                count = Math.floor(since.getTime() / ms);

                if (count != 0) {
                        break;
                }
        }

        return count + ' ' + name + ((count == 1) ? '' : 's') + ' ago';
}

关于javascript - 使用 javascript 定期更新发布评论的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457251/

相关文章:

javascript - 在 angularjs 中的组件绑定(bind) html 属性中使用过滤器(导致 $digest() 错误)

javascript - 尝试对对象数组进行排序但 splice 不是函数?

javascript - 为什么边距的设置会在桌面和移动设备上产生两个不同的值?

jquery - Bootstrap 3 Popover 隐藏在 overflow hidden 的元素后面

c++ - 输出带有数字分组的数字(1000000 为 1,000,000 等等)

php - 自动缩进 HTML 代码并显示它

javascript - react native FlatList keyExtractor 和 listKey

javascript - 按降序按两个值对数组中的对象进行排序

java - 如何编写可分析的线程转储格式

javascript - Jquery ID 选择器不适用于变量