AI Hints
Home›JavaScript Tutorial›Quiz App and Timers
JavaScript · Lesson 104 of 138

Timers and a Clock

Pages that update themselves.

⏱ 2 min read▶ Video at 2:04:28🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Some pages update on their own. A clock, a countdown to a sale, or a slideshow. Set interval runs a function every so often, so the page changes without anyone clicking.

Timers and a Clock
From the video: Timers and a Clock
  • Clocks, countdowns, slideshows
  • No clicks needed
  • setInterval

A live clock

Tick reads the current time with new date, and formats it with to locale time string. Pad start would add zeros, but this method does it for us. We call tick once straight away, then set interval calls it every second. The screenshot shows the clock at the moment it was taken.

index.htmlpython
<link rel="stylesheet" href="style.css">
<h1>Clock</h1>
<p id="clock" class="big"></p>
<script>
  const clock = document.querySelector("#clock");

  function tick() {
    clock.textContent = new Date().toLocaleTimeString("en-GB");
  }

  tick();
  setInterval(tick, 1000);
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}

A countdown with a stop button

Here, a countdown starts at ten, and drops by one every half a second. Clear interval stops it when it reaches zero, or when the stop button is clicked. We waited about a second, then clicked stop, so it froze partway down.

index.htmlpython
<link rel="stylesheet" href="style.css">
<p id="out" class="big">10</p>
<button id="stopBtn">Stop</button>
<script>
  let n = 10;
  const id = setInterval(() => {
    n--;
    out.textContent = n;
    if (n === 0) clearInterval(id);
  }, 500);

  stopBtn.onclick = () => {
    clearInterval(id);
    out.textContent += " (stopped)";
  };
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"wait": 1200}, {"click": "#stopBtn"}]

Summary

Set interval keeps a page updating, and clear interval stops it. Next, a quick quiz on the DOM.

Key takeaways
  • setInterval for updates
  • new Date() for time
  • clearInterval to stop
Next up: DOM Quiz — test yourself