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

时间:2012-05-04 23:13:01

标签: javascript jquery formatting setinterval

我想定期更新发布在我网站上的评论的时间。我对Facebook和其他人感兴趣的格式已经完成了与当前时间相关的帖子时间。例如,帖子应该说,“刚才......”首次提交但是1分钟后应该说“1分钟前”,然后“2分钟前”等等。

我找到了这个很好的脚本:http://forrst.com/posts/Facebook_style_live_dates_in_JavaScript-hro 并且可能已经到了一半。

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

  1. 报告的时间被搞砸了。
  2. 即使setinterval()工作正常,时间也不会每秒更新一次。
  3. 这里是代码的片段,我将其全部放在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); 
    });
    

1 个答案:

答案 0 :(得分:2)

我已经测试了它,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';
}