Javascript DOM How to - Update a HTML tag after an interval








Question

We would like to know how to update a HTML tag after an interval.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from  w  ww  .  j  a  v a 2  s.c o  m-->
    setInterval(function() {
        var baseDate       = new Date(),
            seconds        = baseDate.getSeconds(),
            secondsElement = document.getElementsByClassName("seconds")[0];
        secondsElement.innerHTML = seconds;
    }, 1000);
}
</script>
</head>
<body>
  <div class="seconds"></div>
</body>
</html>

The code above is rendered as follows: