0 thích 0 không thích
62 xem
bởi (1.8k điểm)
hướng dẫn làm đồng hồ với javascript html

1 Trả lời

0 thích 0 không thích
bởi (6.5k điểm)

bạn tham khảo nhé:

<!DOCTYPE html>

<html>

<body onload="startTime()">

<h2>JavaScript Clock</h2>

<div id="txt"></div>

<script>

function startTime() {

  const today = new Date();

  let h = today.getHours();

  let m = today.getMinutes();

  let s = today.getSeconds();

  m = checkTime(m);

  s = checkTime(s);

  document.getElementById('txt').innerHTML =  h + ":" + m + ":" + s;

  setTimeout(startTime, 1000);

}

function checkTime(i) {

  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10

  return i;

}

</script>

</body>

</html>

...