Home / Learn / How do I build an animated countdown timer for a launch or sale?

How do I build an animated countdown timer for a launch or sale?

A countdown draws attention to an offer or launch with a fixed date. We compute the time left from the clock each time, so it stays correct even if the page sleeps, and we animate only the number that changed.

Steps

  1. Set the end time as a fixed date with its timezone, for example new Date("2027-01-01T00:00:00+02:00").
  2. Each second, compute the difference between that date and Date.now(), then split it into days, hours, minutes and seconds. Do not count down by subtracting 1, because setInterval can run later than asked.
  3. Update a box only when its text changed, and restart a short animation class on it (a fade with a small move).
  4. Set the number to direction: ltr and keep the box order as is, so days start on the right in RTL.
  5. Stop the timer at zero, and disable the animation under prefers-reduced-motion.

Sources

The code

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #0b1220; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  .cd { display: flex; gap: 1rem; }                       /* in RTL the first box (days) sits on the right */
  .box { text-align: center; min-width: 5rem; padding: .8rem; border-radius: 12px; background: #1c2740; }
  .num { display: block; font-size: 2.6rem; font-weight: 800; font-variant-numeric: tabular-nums; direction: ltr; }
  .num.tick { animation: tick .35s ease-out; }
  @keyframes tick { from { opacity: .2; transform: translateY(-.15em); } to { opacity: 1; transform: none; } }
  .lbl { font-size: .9rem; opacity: .8; }
  @media (prefers-reduced-motion: reduce) { .num.tick { animation: none; } }
</style>
<div class="cd" role="timer" aria-label="العد التنازلي">
  <div class="box"><span class="num" data-u="d">00</span><span class="lbl">يوم</span></div>
  <div class="box"><span class="num" data-u="h">00</span><span class="lbl">ساعة</span></div>
  <div class="box"><span class="num" data-u="m">00</span><span class="lbl">دقيقة</span></div>
  <div class="box"><span class="num" data-u="s">00</span><span class="lbl">ثانية</span></div>
</div>
<script>
  // Demo target: 3 days from now. In a real page use a fixed date, e.g. new Date('2027-01-01T00:00:00+02:00')
  const target = Date.now() + 3 * 24 * 3600 * 1000;
  const nodes = Object.fromEntries([...document.querySelectorAll('.num')].map(n => [n.dataset.u, n]));
  function set(node, value) {
    const text = String(value).padStart(2, '0');
    if (node.textContent !== text) {          // animate only digits that changed
      node.textContent = text;
      node.classList.remove('tick'); void node.offsetWidth; node.classList.add('tick');
    }
  }
  function update() {
    const left = Math.max(0, target - Date.now());   // always computed from the clock, so it stays correct after the tab sleeps
    const s = Math.floor(left / 1000);
    set(nodes.d, Math.floor(s / 86400));
    set(nodes.h, Math.floor(s % 86400 / 3600));
    set(nodes.m, Math.floor(s % 3600 / 60));
    set(nodes.s, s % 60);
    if (left === 0) clearInterval(timer);
  }
  const timer = setInterval(update, 1000);
  update();
</script>
</html>

can't find the motion you need? Request it here

Related articles: