2011-07-29 49 views
0
<html> 
<head> 
<script type="text/javascript"> 
var c=0; 
var t; 
var timer_is_on=0; 

function timedCount() 
{ 
document.getElementById('txt').value=c; 
c=c+1; 
t=setTimeout("timedCount()",30); 
} 

function doTimer() 
{ 
if (!timer_is_on) 
    { 
    timer_is_on=1; 
    timedCount(); 
    } 
} 
</script> 
</head> 

<body> 
<form> 
<input type="button" value="Start count!" onClick="doTimer()"> 
<input type="text" id="txt"> 
</form> 
<p>Click on the button above. The input field will count forever, starting at 0.</p> 
</body> 
</html> 

我该如何修改这段代码让它停在100? 谢谢!javascript count number

+0

只是一个说明,最好在函数对象中使用而不是字符串中的代码:'setTimeout(timedCount,30)' – mykhal

回答

2
if (c >= 100){ 
    //do stuff 
}else{ 
    t=setTimeout("timedCount()",30); 
} 
0

更换

t=setTimeout("timedCount()",30); 

if(c < 100) { 
    t=setTimeout("timedCount()",30); 
} 

尝试自己做下一次,这一次是真的很容易...

安东尼

0

只需添加这条线位于函数的顶部timedCount

function TimedCount() 
{ 
    if (c>100) return; 

    // use your existing code below 

}