javascript count number

javascript count number

本文关键字:number count javascript      更新时间:2023-09-26
<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?谢谢!

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

Replace

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

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

下次试着自己做,这个真的很简单…

安东尼

在函数TimedCount的顶部添加这一行

function TimedCount ()
{
    if (c>100) return;
    // use your existing code below
}