且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用AJAX运行实时时钟

更新时间:2023-02-26 17:08:11

计算机时钟并非如此精确,以至于您必须每秒重新同步它们.每十分钟,甚至每小时尝试一次.

Computer clocks are not so inaccurate that you have to re-sync them every second. Try every ten minutes, or even every hour.

实际上,我只是完全不进行同步.这样做要容易得多:

In fact, I just do away with synchronising altogether. It is far easier to do this:

<script type="text/javascript">
    TIMESTAMP = <?php echo time(); ?>000;
    offset = new Date().getTime()-TIMESTAMP;
    setInterval(function() {
        var now = new Date();
        now.setTime(now.getTime()-offset);
        // print out the time according to the variable `now`
    },1000);
</script>