I was a little bored, so I made my own annotated version of my code, with some extra markup and styles thrown in as 'eye candy', or whatever (only the script code is annotated):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Counter - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
fieldset {
width:21.5em;
}
#inner {
padding-top:1ex;
}
form {
margin:0;
padding:0;
}
</style>
<script type="text/javascript">
function counter(el){
var o=el.onclick, //create a reference to the element's onclick function for later shorthand use.
e=el.form.elements, //condense the verbose and reused multi-object lookup for the form's elemnts for later shorthand use.
v='value', //create a reference to the value attribute for later shorthand use.
x; //declare x for later use.
if(isNaN(x= +e.txt1[v])) //with below line tests if the value of the txt1 input can be number.
x=0; //if so, it assigns it to x, otherwise sets x to 0.
e.txt[v]=x++; //sets the txt input's value to x, while at the same time incrementing it for its next use.
if(o.t) //with below line - if we are already running,
clearInterval(o.t); //stop to clear the way for a new run.
o.t=setInterval(function(){e.txt[v]=x++;}, 1000); //set a reference to and initiate the repeating loop of
//updating by 1 both x and the txt input's value.
};
</script>
</head>
<body>
<form action="javascript:void(0);" onsubmit="return false;">
<fieldset>
<legend>Counter</legend>
<div id="inner">
<input type="button" value="Start Count!" onclick="counter(this)">
<input name="txt" readonly type="text">
<div>Click the above button. It will count 'forever', starting at:</div>
<input name="txt1" type="text" value="0">
</div>
</fieldset>
</form>
</body>
</html>
Bookmarks