从w3school 看到的。
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
</script>
</head><body><form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
</form><p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p></body></html>
这是一段无休止的计时器
如果想要在某一个时间,比如第五秒的时候终止,代码该怎么添加修改呢?
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
</script>
</head><body><form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
</form><p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p></body></html>
这是一段无休止的计时器
如果想要在某一个时间,比如第五秒的时候终止,代码该怎么添加修改呢?
http://www.w3schools.com/js/js_timing.asp
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
if(c>5) clearTimeout(t);
}
</script>
如果一定要用跳出。。还可以用return
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
if(c>5) clearTimeout(t);
}
</script>