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>

How can I modify this code to let it stop at 100? Thanks!

1
  • just a note, it's better to use a function object rather than a code in string: setTimeout(timedCount, 30) Commented Jul 29, 2011 at 11:41

3 Answers 3

2
if (c >= 100){ 
   //do stuff
}else{
    t=setTimeout("timedCount()",30);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Replace

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

with

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

Try to do it yourself next time, this one was really easy...

Antoine

Comments

0

just add this line at top of function TimedCount

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

    // use your existing code below

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.