Skip to main content

Command Palette

Search for a command to run...

Membuat countdown timer Javascript

Published
5 min read
Membuat countdown timer Javascript
A

Javascript developer

Perusahaan-perusahaan besar biasanya 1 bulan sebelum launching atau bahkan lebih sudah mengumumkan kepada publik di website bisa dalam bentuk video, gambar, bahkan ada dengan countdown timer. Countdown juga sering dipakai untuk menyambut pergantian tahun diakhir tahun menymbut tahun baru, ada juga yang pakai untuk ucapan ulang tahun.

Saat ini saya akan membahas bagaimana cara membuat countdown timer menggunakan javascript?

countdown timer javascript

Siapkan kode html seperti berikut

  <div class="countdown">
      <div class="time">
        <span id="days">00</span>
        <span>days</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="hours">00</span>
        <span>hours</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="minutes">00</span>
        <span>minutes</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="seconds">00</span>
        <span>seconds</span>
      </div>
    </div>

Pada kode html diatas telah diberi id untuk days, hours, minutes, dan seconds yang akan diupdate nilainya ketika ada perubahan dari timer sehingga pada website countdown berjaln dengan baik. Bagian ini akan dilakukan menggunakan DOM javascript. Berikut kode javascriptnya.

function getCounter() {
  var countDownDate = new Date("Aug 11, 2021 22:00:00").getTime();

  var x = setInterval(function () {
    var now = new Date().getTime();

    var distance = countDownDate - now;

    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor(
      (distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
    );
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    var textDays = document.getElementById("days");
    var textHours = document.getElementById("hours");
    var textMinutes = document.getElementById("minutes");
    var textSeconds = document.getElementById("seconds");

    textDays.innerHTML = days < 10 ? "0" + days : days;
    textHours.innerHTML = hours < 10 ? "0" + hours : hours;
    textMinutes.innerHTML = minutes < 10 ? "0" + minutes : minutes;
    textSeconds.innerHTML = seconds < 10 ? "0" + seconds : seconds;

    if (distance < 0) {
      clearInterval(x);
    }
  }, 1000);
}

Hasilnya akan seperti berikut.

no-css.png

Selanjutnya agar terlihat rapi, beri sedikit sentuhan styling css seperti kode berikut.

.countdown {
  margin-bottom: 40px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}
@media only screen and (max-width: 1024px) {
  .countdown {
    justify-content: center;
  }
}
.countdown .time {
  display: flex;
  flex-direction: column;
  justify-content: start;
}
.countdown .time:not(:last-child) {
  margin-right: 16px;
}
.countdown .time #days,
.countdown .time #hours,
.countdown .time #minutes,
.countdown .time #seconds,
.countdown .time .semicolon {
  font-family: Samsung Sharp Sans;
  font-style: normal;
  font-weight: bold;
  letter-spacing: 0.3px;
}
@media only screen and (min-width: 1024px) {
  .countdown .time #days,
  .countdown .time #hours,
  .countdown .time #minutes,
  .countdown .time #seconds,
  .countdown .time .semicolon {
    font-size: 72px;
    line-height: 91px;
  }
}
@media only screen and (max-width: 1024px) {
  .countdown .time #days,
  .countdown .time #hours,
  .countdown .time #minutes,
  .countdown .time #seconds,
  .countdown .time .semicolon {
    font-size: 40px;
    line-height: 50px;
    text-align: center;
  }
}
.countdown .time span {
  font-family: Samsung Sharp Sans;
  font-style: normal;
  font-weight: normal;
  font-size: 12px;
  line-height: 15px;
  text-align: center;
  letter-spacing: 0.3px;
  align-self: center;
}
.countdown .semicolon {
  margin-right: 16px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  font-family: Samsung Sharp Sans;
  font-style: normal;
  font-weight: bold;
  letter-spacing: 0.3px;
}
@media only screen and (min-width: 1024px) {
  .countdown .semicolon {
    font-size: 72px;
    line-height: 91px;
  }
}
@media only screen and (max-width: 1024px) {
  .countdown .semicolon {
    font-size: 40px;
    line-height: 50px;
    text-align: center;
  }
}

Maka hasilnya seperti berikut.

countdown.png

Berikut kode secara lengkapnya.

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
  <title>countdown</title>
  <link href="//fonts.cdnfonts.com/css/samsung-sharp-sans" rel="stylesheet" />
  <style>
    .countdown {
      margin-bottom: 40px;
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
    }

    @media only screen and (max-width: 1024px) {
      .countdown {
        justify-content: center;
      }
    }

    .countdown .time {
      display: flex;
      flex-direction: column;
      justify-content: start;
    }

    .countdown .time:not(:last-child) {
      margin-right: 16px;
    }

    .countdown .time #days,
    .countdown .time #hours,
    .countdown .time #minutes,
    .countdown .time #seconds,
    .countdown .time .semicolon {
      font-family: Samsung Sharp Sans;
      font-style: normal;
      font-weight: bold;
      letter-spacing: 0.3px;
    }

    @media only screen and (min-width: 1024px) {

      .countdown .time #days,
      .countdown .time #hours,
      .countdown .time #minutes,
      .countdown .time #seconds,
      .countdown .time .semicolon {
        font-size: 72px;
        line-height: 91px;
      }
    }

    @media only screen and (max-width: 1024px) {

      .countdown .time #days,
      .countdown .time #hours,
      .countdown .time #minutes,
      .countdown .time #seconds,
      .countdown .time .semicolon {
        font-size: 40px;
        line-height: 50px;
        text-align: center;
      }
    }

    .countdown .time span {
      font-family: Samsung Sharp Sans;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 15px;
      text-align: center;
      letter-spacing: 0.3px;
      align-self: center;
    }

    .countdown .semicolon {
      margin-right: 16px;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      font-family: Samsung Sharp Sans;
      font-style: normal;
      font-weight: bold;
      letter-spacing: 0.3px;
    }

    @media only screen and (min-width: 1024px) {
      .countdown .semicolon {
        font-size: 72px;
        line-height: 91px;
      }
    }

    @media only screen and (max-width: 1024px) {
      .countdown .semicolon {
        font-size: 40px;
        line-height: 50px;
        text-align: center;
      }
    }
  </style>
</head>

<body>
  <div class="hero">
    <div class="countdown">
      <div class="time">
        <span id="days">00</span>
        <span>days</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="hours">00</span>
        <span>hours</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="minutes">00</span>
        <span>minutes</span>
      </div>
      <div class="semicolon">:</div>
      <div class="time">
        <span id="seconds">00</span>
        <span>seconds</span>
      </div>
    </div>
  </div>
  <script>
    function getCounter() {
      var countDownDate = new Date("Aug 11, 2021 22:00:00").getTime();

      var x = setInterval(function () {
        var now = new Date().getTime();

        var distance = countDownDate - now;

        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor(
          (distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
        );
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        var textDays = document.getElementById("days");
        var textHours = document.getElementById("hours");
        var textMinutes = document.getElementById("minutes");
        var textSeconds = document.getElementById("seconds");

        textDays.innerHTML = days < 10 ? "0" + days : days;
        textHours.innerHTML = hours < 10 ? "0" + hours : hours;
        textMinutes.innerHTML = minutes < 10 ? "0" + minutes : minutes;
        textSeconds.innerHTML = seconds < 10 ? "0" + seconds : seconds;

        if (distance < 0) {
          clearInterval(x);
        }
      }, 1000);
    }

    getCounter();

  </script>
</body>

</html>
F
Fernn3y ago

js nya udah ga bisa ya? udah saya ubah value nya kok masih gak jalan count down nya

A
Asrul H3y ago

harusnya masih bisa, boleh SS error yg Fernndapatkan?

F
Fernn3y ago

Asrul H tampilan normal, css sama html nya udah sesuai, tapi countdown js nya aja yg ga jalan.. jadi 00:00:00:00 gitu

A
Agung3y ago

Kode untuk menampilkan text saat couldown timer berakhir gmn itu kak?

A
Asrul H3y ago

kalau ngasi teks untuk coundown yg saat berakhir bisa gunakan append element pada dom yg akan dimunculin, atau hanya sekedar alert tambahkan alert("message")