simonjvardy / alarm-clock

Alarm Clock app using HTML5, CSS3 and JavaScript. Code Institute Milestone 2 Project.
https://simonjvardy.github.io/alarm-clock/
6 stars 8 forks source link

Fix: Issue #45 .toLocaleTimeString() format differences #46

Closed simonjvardy closed 3 years ago

simonjvardy commented 3 years ago

Replace .toLocaleTimeString() with string concatenation from the Date() object hours, minutes and seconds values. This created a more stable cross-browser support - tested on Chrome, Firefox and Edge.

simonjvardy commented 3 years ago

replaced alarmTime() function code:

function alarmTime() {
  let now = new Date();
  /*
  Return the time portion of a Date object as a string
  in the format "hh:mm:ss"
  */
  currentTime = now.toLocaleTimeString();
  if (currentTime === alarmElement) {
    alarmDisplay.play();

    // Assign css class to bell icon to make the image shake
    bellIconDiv.classList.add("bell-icon-shake");
  }
  setTimeout(alarmTime, 1000);
}

as the now.toLocaleTimeString() is returned in different time formats depending on the developer to this function code:

function alarmTime() {
    /*
  Return the time portion of a Date object as a string
  in the format "hh:mm:ss"
  */
  let currentDate = new Date();
  let hour = currentDate.getHours();
  let minute = currentDate.getMinutes();
  let seconds = currentDate.getSeconds();
  let currentTime = String(hour) + ":" + String(minute) + ":" + String(seconds);
  if (currentTime === alarmElement) {
    alarmDisplay.play();

    // Assign css class to bell icon to make the image shake
    bellIconDiv.classList.add("bell-icon-shake");
  }
  setTimeout(alarmTime, 1000);
}