what’s the best way to use setinterval as a clock update in seconds?

Put all your code in a function and then call it every seconde with setInterval

function updateTime () {
  let currentDateTime = new Date();

  hours = ('0'+currentDateTime.getHours()).slice(-2);
  mins = ('0'+currentDateTime.getMinutes()).slice(-2);

  let formattedTime = hours + ":" + mins;

  $('#time').html(formattedTime)
}

setInterval(updateTime, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="time"></div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top