0

Fiddle

I am making a social network and when you post something, I want to show the time. I have the current code to display the time (this isn't only the time, it's also validating and posting the text):

$('#b').click(function () {
    var v = $('#type').val();
    var u = $('#input').val();
    if (v !== "" && u !== ""){
        var time = new Date();
        var currentime = Date.now();
        var x = currentime - time;
        $("ul").prepend("<li>" + v + "<br />Posted by " + u + " " + x +" minutes ago  </li>");
        $('#type, #input').css('border','');
    }
    else if (v == "" && u == "") {
        $('#type, #input').css('border','1px solid red');
    }
    else if (v == "") {
        $('#type').css('border','1px solid red');
        $('#input').css('border','');
    }
    else {
        $('#input').css('border','1px solid red');
        $('#type').css('border','');
    }
});

When the time posts, it says 0 minutes ago (like I told it to), but after any amount of time, it still says 0.

4

1 回答 1

1

小提琴

基本上,您设置了一个setInterval来管理您是否经常更新时间属性。我使用了span一个类.time,所以理论上你可以随着时间的推移更新任何东西。

我会更进一步并散列您的帖子,以便您可以轻松地检索每个帖子的原始时间。

编辑:添加了一个新的小提琴。

于 2013-11-30T15:55:55.753 回答