2011-07-29 6 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에서 멈출 수 있습니까? 감사합니다.자바 스크립트 카운트 번호

+0

참고로 문자열의 코드보다는 함수 객체를 사용하는 것이 더 좋습니다.'setTimeout (timedCount, 30)' – mykhal

답변

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

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

시도와

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

이 다음에 너 자신을 바꾸기를,이 사람은 그냥 ... 추가 앙투안

0

정말 쉬웠다 함수의 맨 위에있는이 줄 TimedCount

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

    // use your existing code below 

} 
관련 문제