1.用js代码动态获取服务器的时间,并显示在页面的时候,时间的出现会有大概1秒多的延迟。如果按F5键不停刷新的话,时间就不会显示了。用原生javascript或者jQuery都是这样,求大神解答问题出在哪里了?
下面是代码:
<!DOCTYPE HTML>
<html>
<title>动态获取服务器的时间</title>
<meta charset="utf-8">
<head>
<!-- <script src="http://code.jquery.com/jquery-latest.js"></script> -->
</head>
<body>
当前服务器时间:<span id="time"></span>
</body>
<script>
console.log(document.getElementById("time"));
function current(){
var d=new Date(),str='';
str +=d.getFullYear()+'年';
str +=d.getMonth()+1+'月';
str +=d.getDate()+'日';
str +=d.getHours()+'时';
str +=d.getMinutes()+'分';
str +=d.getSeconds()+'秒';
return str;
}
//setInterval(function(){$('#time').html(current())},1000);
setInterval(function(){
document.getElementById("time").innerHTML=current();
},1000);
</script>
</html>
杨__羊羊
相关分类