我有这个脚本time.js
function changeTime(){
$('p.text-muted').each(function(index, el) {
$str = moment($(el).text()).fromNow();
console.log($str);
$(el).html('<i class="fa fa-clock-o"></i> '+$str);
});
}
changeTime();
setInterval(update, 1000*60);
这是我打电话changeTime()
的地方之一:
<p class="small text-muted" id="t<?php echo($tweet['tid']);?>"><i class="fa fa-clock-o"></i> <?php echo($tweet['ttime']); ?></p>
第一次调用changeTime()
时,值设置正确。但是后来,它给出了“几秒钟前”。
我理解在以后的电话中,它变成片刻('4天前')的问题。 fromNow()
变成“几秒钟前”。那么如何将此4 days ago
转换为实际时间,然后再次调用.fromNow()
?我找不到确切的方法。请帮忙。
谢谢:)
答案 0 :(得分:2)
首先,您粘贴的代码setInterval(update, 1000*60);
中存在错误,应该是setInterval(changeTime, 1000*60);
其次请在属性中保存时间戳,而不是在div
中<p class="small text-muted" id="t<?php echo($tweet['tid']);?>" data-timestamp="<?php echo($tweet['ttime']); ?>"><i class="fa fa-clock-o"></i> <?php echo($tweet['ttime']); ?></p>
更新了JS代码: -
function changeTime(){
$('p.text-muted').each(function(index, el) {
$str = moment($(el).data('timestamp')).fromNow();
console.log($str);
$(el).html('<i class="fa fa-clock-o"></i> '+$str);
});
}
changeTime();
setInterval(changeTime, 1000*60);