这次给大家带来js实现以秒为单位刷新时间,js实现以秒为单位刷新时间的注意事项有哪些,下面就是实战案例,一起来看一下。
原理:使用定时器,即setInterval(fn,i),每隔i秒执行fn。
下面给出具体的代码
1.代码如下:
<span>nbsp;html> <meta> <title>用js实现每隔一秒刷新时间(含年月日时分秒)</title> <style> #time{background:#33F; color:white; height:30px; line-height:30px; padding:20px; font-size:18px; width:400px; text-align:center; margin:0 auto; margin-top:200px;} </style> <p></p> <script> setInterval(function(){ var time=new Date(); var year=time.getFullYear(); //获取年份 var month=time.getMonth()+1; //获取月份 var day=time.getDate(); //获取日期 var hour=checkTime(time.getHours()); //获取时 var minite=checkTime(time.getMinutes()); //获取分 var second=checkTime(time.getSeconds()); //获取秒 /****当时、分、秒、小于10时,则添加0****/ function checkTime(i){ if(i<10) return "0"+i; return i; } var box=document.getElementById("time"); box.innerHTML=year+"年"+month+"月"+day+"日 "+hour+":"+minite+":"+second; },1000); //setInterval(fn,i) 定时器,每隔i秒执行fn </script></span><span> </span>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是js实现以秒为单位刷新时间的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号