Categories
Mastering Development

Is it possible to override a clock function so I could change its hours?

I’m designing a small browser project where you pick a city from the list and it presents you with the current time in city of your choice.
Although I’ve come across a problem.
The code only shows the correct time for a brief moment and then returns to the original UTC Standard time. The problem comes up at the “setTimeout” bit. Is there a possibility to override this? By adding clearTimeout perhaps?

function utcStandard(offset) {
  var date = new Date();
  var hh = date.getUTCHours();
  var mm = date.getUTCMinutes();
  var ss = date.getUTCSeconds();
  if (offset != undefined) {
    hh += offset
  };
  hh = checkTime(hh);
  mm = checkTime(mm);
  ss = checkTime(ss);
  document.getElementById("time").innerHTML =
    hh + ":" + mm + ":" + ss;
  var t = setTimeout(utcStandard, 500);

  function checkTime(i) {
    if (i < 10) {
      i = "0" + i
    };
    return i;
  }
}

I decided to add the function to each item in the HTML list. For example.

<ul>
  <li><a href="#" onclick="utcStandard(1)">Amsterdam</a></li>
</ul>

Leave a Reply

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