我被分配了一个向用户显示服务器时钟的任务。我可以使用jquery做到这一点,但我们需要显示我们的服务器时钟,就像一个时钟。
就像我的日期格式类似:2016-06-30 10:27:32
,它应该像计时器或时钟一样运行。我在互联网上搜索了很多,但我没有得到任何线索。
请建议我解决方案。
答案 0 :(得分:0)
可能最好的解决方案是从服务器获取时间/日期并通过jquery / JS递增它。
<?php $currentTime = getdate(); ?>
<script>
var date = new Date(Date.UTC(<?php echo $currentTime['year'] .",".
$currentTime['mon'] .",".
$currentTime['mday'] .",".
$currentTime['hours'] .",".
$currentTime['minutes'] .",".
$currentTime['seconds']; ?>));
setInterval(function() {
date.setSeconds(date.getSeconds() + 1);
console.log((date.getHours() +':' + date.getMinutes() + ':' + date.getSeconds() ));
}, 1000);
</script>
因为setInterval不是最精确的函数,所以你应该通过AJAX请求每分钟更新一次日期。