Adam Gardner
Adam Gardner

Reputation: 1276

JavaScript time in text field and not updating

Not sure why this JavaScript inst placing the time within the blank text field and updating the time every second. I am trying to get the JavaScript to have a label at the top that says Current Time then a text field that has the current time that is updated every second.

    <html>
<head>
<title>Timer</title>
<script language="javascript" type="text/javascript">
var dateform
speed=2000
tid=0;
function dodate()
{
f.date.value=new Date();
tid=window.setTimeout("dodate()",speed);
}
function start(x)
{
  f=x 
  tid=window.setTimeout("dodate()",speed);
}
function cleartid()
{
  window.clearTimeout(tid);
}
</script>
</head>
<body onload="start(document.dateform) ;">
<center>
The Digital Clock
<FORM name="dateform" action="post">
<input type="text" name="date" size=30>
</FORM>
</center>
</body>
</html>

Upvotes: 1

Views: 61

Answers (1)

ifm
ifm

Reputation: 1196

The method you need to use is setInterval and make little changes:

var dateform
speed=1000
tid=0;
function dodate()
{
f.date.value=new Date();
}
function start(x)
{
  f=x 
  tid=window.setInterval("dodate()",speed);
}
function cleartid()
{
  window.clearTimeout(tid);
}

Upvotes: 2

Related Questions