搜索了网上显示实时时间的代码,有的是用setInterval来写的,通过setInterval调用日期函数,达到在页面动态显示实时时间的目的,这样写发现一个问题,进入页面需要1秒的延迟,体验上稍微差一点,稍微改了一下代码取消掉这种延时。
$(function () {CurrentTime();})function CurrentTime() {var date = new Date();var year = date.getFullYear();var month = date.getMonth() + 1;var day = date.getDate();var hour = date.getHours();var minute = date.getMinutes();var second = date.getSeconds();month = month < 10 ? ("0" + month) : month;day = day < 10 ? ("0" + day) : day;hour = hour < 10 ? ("0" + hour) : hour;minute = minute < 10 ? ("0" + minute) : minute;second = second < 10 ? ("0" + second) : second;var Timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;//在页面上插入日期$("#Time").html(Timer);setTimeout(function () {CurrentTime();}, 1000);}