When I press the button onces, I want to start count, and when I press again to stop count. I know that to work two functions with one button onclick event, it is seperate with “;”. Any suggestion? This code works well with two buttons each for one function.
Thanks in advance.
<form>
<input type="button" value="Start count!" onclick="doTimer();stopCount();" />
</form>
javascript code:
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
Here’s my untested solution with a few tips thrown in for good measure.
First, I think you can probably get rid of the
<form>and</form>tags.Second, you may find it helpful to give your variables meaningful names, not just
cortThird, change timer is on to a boolean:
var timer_is_on = false;Fouth change the button to this:
<input type="button" value="Start count!" onclick="doTimer();" />Fifth replace your code with this: