分享一个简单实用的前台计时器
2016-10-26 19:03
274 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 最近做了一个前端的计时器,JS实现的,感觉还不错,和大家分享一下:</span>
javascript里的代码:
var c=0 var t var hour=0 var min = 0 var lastsecs = 0 function timedCount() { <span style="white-space:pre"> </span>var temptextmin=document.getElementById('txt'); <span style="white-space:pre"> </span>hour = parseInt(c / 3600);// 小时数 <span style="white-space:pre"> </span>min = parseInt(c / 60);// 分钟数 <span style="white-space:pre"> </span>if(min>=60){ <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>min=min%60 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>lastsecs = c % 60; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>temptextmin.value = hour + "时" + min + "分" + lastsecs + "秒" <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>c=c+1 <span style="white-space:pre"> </span>t=setTimeout("timedCount()",1000) <span style="white-space:pre"> </span>document.getElementById('start').style.display = "none"; <span style="white-space:pre"> </span>document.getElementById('end').style.display = ""; }
//停止计时
function stopCount() { <span style="white-space:pre"> </span>clearTimeout(t) <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>document.getElementById('start').style.display = ""; <span style="white-space:pre"> </span>document.getElementById('end').style.display = "none"; <span style="white-space:pre"> </span>c=0 <span style="white-space:pre"> </span>document.getElementById('txt').value= "0时" + "0分" + "0秒" <span style="white-space:pre"> </span>clearTimeout(t) <span style="white-space:pre"> </span>document.getElementById('start').style.display =""; <span style="white-space:pre"> </span>document.getElementById('end').style.display = "none"; }
body中的代码:
<body> <input type="text" id="txt" value="0时0分0秒" style="text-align: center;width: 200px;height: 60px; background: black; font-size: 220%;color: white;"> <input type="button" value="开始计时" id="start" onClick="timedCount()" class="button pink" style="height: 40px;width: 130px;font-size:150%" > <input type="button" value="停止计时" class="button pink" style="font-size:150%;height: 40px;width: 130px;display: none" id="end" onClick="stopCount()"> </body>
效果如下:
相关文章推荐
- 分享一个简单而实用的缩略图方法
- DIY一个高大上带提醒的计时器,简单实用,你还在等什么
- 分享一个php验证码程序 简单 实用
- DIY一个高大上带提醒的计时器,简单实用,你还在等什么
- Android开发中一个简单实用的调试应用技巧分享
- DIY一个高大上带提醒的计时器,简单实用,你还在等什么
- 分享一个php验证码程序 简单 实用
- 一个简单实用的sql数据库查询工具(c#)
- 一个简单的实用的log4j.properties
- 亲手焙制一个极其简单但却极其实用的Reflector插件
- 一个简单实用的数据库操作框架
- [分享+教程]迁移到Ubuntu,打造一个实用且漂亮的系统!
- 一个简单的Symbian OS异步计时器
- 一个简单实用的单边对联广告
- 一个简单实用的数据访问层
- 一个简单实用的保护视力的方法(IT人士必看的方法)
- 经验技巧分享--ASP.NET和Ajax应用一个超级实用的设计模式---享元模式
- 亲手焙制一个极其简单但却极其实用的Reflector插件
- 一个简单的Symbian OS异步计时器
- [原创]一个简单实用的rss浏览脚本,也可以打开远程RSS.