Digital Clock

Concepts Used: setInterval(), DOM update

Features:

  • Shows live time updating every second
<!DOCTYPE html>
<html>
<head>
  <title>Digital Clock</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <style>
#clock { font-size: 40px; font-weight: bold; margin: 20px; }
</style> </head> <body> <h2>Digital Clock</h2> <div id="clock"></div> <script>
$(document).ready(function(){
  function updateClock(){
    let now = new Date();
    let time = now.toLocaleTimeString();
    $("#clock").text(time);
  }
  setInterval(updateClock, 1000);
  updateClock();
});
</script> </body> </html>

Explanation:

  • setInterval() updates every second.
  • .text() replaces content with the current time.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *