Skip to content

Instantly share code, notes, and snippets.

@FrancoB411
Created December 13, 2011 06:57
Show Gist options
  • Select an option

  • Save FrancoB411/1470980 to your computer and use it in GitHub Desktop.

Select an option

Save FrancoB411/1470980 to your computer and use it in GitHub Desktop.
Simple Javascript Counter
<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()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
timer_is_on=0;
document.getElementById('txt').value=c;
}
function resetCount()
{
c=0
document.getElementById('txt').value=c;
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onclick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onclick="stopCount()" />
<input type="button" value="Reset count!" onclick="resetCount()" />
</form>
<p>
Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment