2011-07-29 9 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のカウント数

+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); 

を交換し、この1つは本当に簡単だった...

アントワーヌ

0

がちょうど追加関数の先頭にあるこの行TimedCount

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

    // use your existing code below 

} 
関連する問題