dimanche 12 juin 2016

Why does my javascript clock crash after a minute?

I have double checked and tested on jshint for syntax errors and such. I have also compared my code to others who created the same clock, but did not see any differences that would cause my clock to crash. I do not understand what is causing this problem.

$(document).ready(function(){

  function displayTime() {
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    var miridiem = "AM";

    var clockDiv = document.getElementById('clock');

    if(seconds < 10) {
      seconds = "0" + seconds
    }

    if(minutes < 10) {
      minutes = "0" + minutes
    }

    if (hours > 12) {
      hours = hours - 12
      miridiem = "PM"
    }

    if (hours === 0) {
      hours = 12
    }

    clockDiv.textContent = hours + ":" + minutes + ":" + seconds + " " + miridiem;

    setInterval(displayTime, 1000);
  }

  displayTime();
});
<body>
 
  <div id="clock"></div>

  <script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
</body>
Warning: snippet will eat your memory. Don't forget to stop it.

https://jsfiddle.net/9cp9m43h/

Aucun commentaire:

Enregistrer un commentaire