首页 > 代码库 > javacript计时

javacript计时

简单的计时:

var t=setTimeout("alert(‘5 秒!‘)",5000)

 

无限计时:

var c=0
var t
function timedCount()
{
document.getElementById(‘txt‘).value=http://www.mamicode.com/c"timedCount()",1000)
}

实现一个暂停计时和重新开始计时的效果:

<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById(‘txt‘).value=http://www.mamicode.com/c"timedCount()",1000)
}

function stopCount()
{
c=0; //1
setTimeout("document.getElementById(‘txt‘).value=http://www.mamicode.com/0",0) //2
/*若果上面2句不加就是一个暂停计时效果,加了就是重新计时效果*/
clearTimeout(t);
}
</script>
</head>

<body>

<form>
<input type="button" value="http://www.mamicode.com/开始计时!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="http://www.mamicode.com/停止计时!" onClick="stopCount()">
</form>

</body>

</html>

 实现一个时钟:

function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById(‘txt‘).innerHTML=h+":"+m+":"+s
t=setTimeout(‘startTime()‘,500)
}

function checkTime(i)
{
if (i<10) 
  {i="0" + i}
  return i
}

 

javacript计时