Hi,
I have a beginner question here. I read the tutorial and from what I understand the setTimeout is only execute once after specified number of miliseconds. If that so, how can the below code keep executing (when I click the button, the timer keep counting).
Thanks in advance
<!DOCTYPE html>
<html>
<head>
<script>
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout(function(){timedCount()},1000);
}
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>
</body>
</html>
I have a beginner question here. I read the tutorial and from what I understand the setTimeout is only execute once after specified number of miliseconds. If that so, how can the below code keep executing (when I click the button, the timer keep counting).
Thanks in advance
<!DOCTYPE html>
<html>
<head>
<script>
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout(function(){timedCount()},1000);
}
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>
</body>
</html>