js计时器(timer) 和 js时钟(clock)

记录一下,以防以后用到,搜罗自网络,兼容IE和Firefox。 js 计时器 (timer) ------------------------------------------------------------------------------------------------- html head title Nonove js timer 计时器 /title /head body script langu

记录一下,以防以后用到,搜罗自网络,兼容IE和Firefox。

js 计时器 (timer)
Html代码   收藏代码
  1. <html>  
  2. <head>  
  3. <title> Nonove js timer 计时器 </title>  
  4. </head>  
  5. <body>  
  6. <script language="javascript">  
  7. var se,m=0,h=0,s=0,ss=1;  
  8. function second(){  
  9. if((ss%100)==0){s+=1;ss=1;}  
  10. if(s>0 && (s%60)==0){m+=1;s=0;}  
  11. if(m>0 && (m%60)==0){h+=1;m=0;}  
  12. t=h+"时"+m+"分"+s+"秒"+ss+"毫秒";  
  13. document.getElementById("showtime").value=t;  
  14. ss+=1;  
  15. }  
  16. function startclock(){se=setInterval("second()",1);}  
  17. function pauseclock(){clearInterval(se);}  
  18. function stopclock(){clearInterval(se);ss=1;m=h=s=0;}  
  19. </script>  
  20. <input name="s" type="button" value="开始计时" onclick="startclock()">  
  21. <input name="s" type="button" value="暂停计时" onclick="pauseclock()">  
  22. <input name="s" type="button" value="停止计时" onclick="stopclock()">  
  23.   
  24. <input name="showtime" style="color:#ff0000;width:200px;" id="showtime" type="text" value="0时0分0秒">  
  25. </body>  
  26. </html>  


js 时钟(clock) 兼容IE和Firefox
Html代码   收藏代码
  1. <html>  
  2. <head>  
  3. <title> Nonove js clock 时钟 </title>  
  4. <script type="text/javascript">  
  5. function Clock() {  
  6. var date = new Date();  
  7. this.year = date.getFullYear();  
  8. this.month = date.getMonth() + 1;  
  9. this.date = date.getDate();  
  10. this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];  
  11. this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();  
  12. this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();  
  13. this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();  
  14.   
  15. this.toString = function() {  
  16. return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;  
  17. };  
  18.   
  19. this.toSimpleDate = function() {  
  20. return this.year + "-" + this.month + "-" + this.date;  
  21. };  
  22.   
  23. this.toDetailDate = function() {  
  24. return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;  
  25. };  
  26.   
  27. this.display = function(ele) {  
  28. var clock = new Clock();  
  29. ele.innerHTML = clock.toString();  
  30. window.setTimeout(function() {clock.display(ele);}, 1000);  
  31. };  
  32. }  
  33. </script>  
  34. </head>  
  35. <body>  
  36. <div id="clock" align="center"></div>  
  37. <script type="text/javascript">  
  38. var clock = new Clock();  
  39. clock.display(document.getElementById("clock"));  
  40. </script>  
  41. </body>  
  42. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值