-1

Hello I learned how to get new date , hours and stuff, today i made a simple clock this is fine but i need to reload the page to get the current time, so how can i make it more realistic, i mean to display it as it's counting, would be much better

$(function() {
  var d = new Date(),
    currentDay = d.getDate(),
    hour = d.getHours(),
    minutes = d.getMinutes(),
    seconds = d.getSeconds();

  if (hour < 10) {
    hour = '0' + hour;
  }
  if (minutes < 10) {
    minutes = '0' + minutes;
  }

  document.write(hour + ' : ' + minutes);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Answers1

1

You can use setInterval function, and since you are showing minutes you can call it every 60 sec setInterval

$(function() {
  
  function setTime(){
  var d = new Date(),
    currentDay = d.getDate(),
    hour = d.getHours(),
    minutes = d.getMinutes(),
    seconds = d.getSeconds();
    if (hour < 10) {
    hour = '0' + hour;
    }
    if (minutes < 10) {
      minutes = '0' + minutes;
    }

    document.write(hour + ' : ' + minutes);
  }
  
  setTime();
  setInterval(setTime,60000);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37