gpt4 book ai didi

php - 如何使用 JS 客户端更新时间

转载 作者:行者123 更新时间:2023-11-30 07:16:39 25 4
gpt4 key购买 nike

我想更新页面上记录 li 或 divs 上显示的时间。我已将创建此记录的时间放在每条记录的隐藏字段中。

例子:

# Hi what are you doing? 
5 minute ago. <hidden id='1' value='1345888475'>

# Akon song is good
10 minute ago <hidden id='2' value='1345888855'>

# I love the way you lie
15 minute ago <hidden id='3' value='1345889995'>

如何每分钟更新一次时间?所以一分钟后第一个 div 应该说 6 分钟前第二个会说 11 分钟前?

用 JS 可以吗?我不想为此使用 php。只是使用 JS。数据库中的时间存储为 strtotime(date("Y-m-d H:i:s"))

谢谢!

最佳答案

我个人会像这样使用 HTML:

<span data-time="1345888475">Loading...</span>

然后在你的脚本中有类似的东西:

(function() {
var timetostring(secs) {
if( secs < 60) return secs+" seconds ago";
secs = Math.floor(secs/60);
if( secs < 60) return secs+" minutes ago";
secs = Math.floor(secs/60);
if( secs < 24) return secs+" hours ago";
secs = Math.floor(secs/24);
return secs+" days"; // you can of course continue
};
(function() {
var qsa = document.querySelectorAll("[data-time]"), l = qsa.length, i, t;
for(i=0; i<l; i++) {
t = new Date().getTime()/1000-qsa[i].getAttribute("data-time");
qsa[i].firstChild.nodeValue = timetostring(t);
}
setTimeout(arguments.callee,15000);
});
})();

关于php - 如何使用 JS 客户端更新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com