在JS中获取经过的时间 [英] Getting Elapsed Time in JS
问题描述
我正在建立一个社交网络,当您发布内容时,我想显示时间.我有当前的代码来显示时间(这不仅是时间,它还在验证和发布文本):
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','');
}
});
时间到了,它说的是0分钟前(就像我告诉过的那样),但是经过一定时间后,它仍然是0.
When the time posts, it says 0 minutes ago (like I told it to), but after any amount of time, it still says 0.
推荐答案
基本上,您可以设置 setInterval 通常您会更新时间属性.我将span
与类.time
一起使用,因此从理论上讲,您可以随时间更新任何内容.
Basically you set up a setInterval which governs ho frequently you update your time attribute. I used a span
with a class .time
to so you could theoretically update anything with time.
我会进一步对您的帖子进行哈希处理,以便您可以轻松地检索每个帖子的原始时间.
I would go further and hash your posts so you can easily retrieve each posts original time.
添加了一个新的小提琴.
added a new fiddle.
这篇关于在JS中获取经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!