Javascript Data Type How to - Repeat a function with a clock








Question

We would like to know how to repeat a function with a clock.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--   w ww  .j  av a  2 s  .c  o m-->
<body>
  <div id="clock"></div>
  <script type="text/javascript">
function updateclock(){
    ourDate = new Date();
    document.getElementById("clock").innerHTML = ourDate.toLocaleString();
}
function startclock() {
    updateclock();
    setInterval(updateclock, 1000); 
}
startclock();
</script>
</body>
</html>

The code above is rendered as follows: