Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <h1>COUNTDOWN TIMER</h1>
  <div id="demo"></div>

  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-javascript-countdown-timer/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) WRAPPER */
.countdown * {
  font-family: Impact, sans-serif;
  box-sizing: border-box;
  text-align: center;
}
.countdown {
  display: flex;
  margin: 0 auto;
  min-width: 500px;
}

/* (B) DAY/HR/MIN/SEC CELLS */
.countdown .cell {
  flex-grow: 1;
  flex-basis: 0;
  padding: 10px;
}
.countdown .digits {
  font-size: 24px;
  color: #000;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
}
.countdown .text {
  margin-top: 10px;
  color: #ddd;
}

/* (C) IF YOU WANT TO SPECIFICALLY STYLE THE DAY/HR/MIN/SEC
.countdown .days { }
.countdown .hours { }
.countdown .minutes { }
.countdown .seconds { }
*/

/* (X) DOES NOT MATTER */
/* PAGE & BODY */
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1637775297509-19767f6fc225?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTY5OTQwNg&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
h1 { margin: 10px 0; }
h1, #code-boxx, #code-boxx * {
  font-family: arial, sans-serif;
  color: #fff;
  text-align: center;
}

/* WIDGET */
.widget-wrap {
  padding: 40px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.5);
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                var counter = {
  // (A) HELPER - CREATE HR/MIN/SEC CELL
  //  txt : text for the cell (all small letters)
  square : (txt) => {
    let cell = document.createElement("div");
    cell.className = `cell ${txt}`;
    cell.innerHTML = `<div class="digits">0</div><div class="text">${txt}</div>`;
    return cell;
  },

  // (B) INITIALIZE COUNTDOWN TIMER
  //  target : target html container
  //  remain : seconds to countdown
  //  after : function, do this when countdown end (optional)
  attach : (instance) => {
    // (B1) GENERATE HTML
    instance.target.className = "countdown";
    if (instance.remain >= 86400) {
      instance.target.appendChild(counter.square("days"));
      instance.days = instance.target.querySelector(".days .digits");
    }
    if (instance.remain >= 3600) {
      instance.target.appendChild(counter.square("hours"));
      instance.hours = instance.target.querySelector(".hours .digits");
    }
    if (instance.remain >= 60) {
      instance.target.appendChild(counter.square("mins"));
      instance.mins = instance.target.querySelector(".mins .digits");
    }
    instance.target.appendChild(counter.square("secs"));
    instance.secs = instance.target.querySelector(".secs .digits");

    // (B2) TIMER
    instance.timer = setInterval(() => { counter.ticker(instance); }, 1000);
  },

  // (C) COUNTDOWN TICKER
  ticker : (instance) => {
    // (C1) TIMER STOP
    instance.remain--;
    if (instance.remain<=0) {
      clearInterval(instance.timer);
      instance.remain = 0;
      if (typeof instance.after == "function") { instance.after(); }
    }

    // (C2) CALCULATE REMAINING DAYS/HOURS/MINS/SECS
    // 1 day = 60 secs * 60 mins * 24 hrs = 86400 secs
    // 1 hr = 60 secs * 60 mins = 3600 secs
    // 1 min = 60 secs
    let secs = instance.remain;
    let days = Math.floor(secs / 86400);
    secs -= days * 86400;
    let hours = Math.floor(secs / 3600);
    secs -= hours * 3600;
    let mins  = Math.floor(secs / 60);
    secs -= mins * 60;

    // (C3) UPDATE HTML
    instance.secs.innerHTML = secs;
    if (instance.mins !== undefined) { instance.mins.innerHTML = mins; }
    if (instance.hours !== undefined) { instance.hours.innerHTML = hours; }
    if (instance.days !== undefined) { instance.days.innerHTML = days; }
  },

  // (D) HELPER - CONVERT DATE/TIME TO REMAINING SECONDS
  //  till : (date object) countdown to this date/time
  toSecs : (till) => {
    till = Math.floor(till / 1000);
    let remain = till - Math.floor(Date.now() / 1000);
    return remain<0 ? 0 : remain;
  }
};

// (E) ATTACH COUNTDOWN TIMER
window.onload = () => {
  counter.attach({
    // TARGET + REMAINING TIME
    target : document.getElementById("demo"),
    remain : 86500,
    
    // COUNTDOWN TO SPECIFIC DATE
    // remain : counter.toSecs(new Date("YYYY-MM-DD")),
    
    // OPTIONAL - RUN THIS ON TIMER END
    after : () => { alert("TIMER HAS ENDED!"); }
  });
};
              
            
!
999px

Console