This tutorial covers two options for adding a countdown hero to your project, one based solely in the Visual Editor that uses a built-in template, and for those who want more fine-grained control, a custom code option.
Skill set: Intermediate
Area: UI OR code
Video Length: 21 sec
- You'll need a Builder account.
- An evergreen browser; that is, a modern, up-to-date browser such as Google Chrome.
Use the Countdown featured template, as in the video:
- Go the Insert tab > Featured Templates
- Click View All Templates.
- Search for "countdown".
- Choose one of the countdown templates.
If you have access to code features, you can create a countdown with custom code. Note that we recommend that a developer finetune this example implementation.
In this example, the days text box is bound to state.days
, the hours is bound to state.hours
, the minutes are bound to state.minutes
, and the seconds are bound to state.seconds
.
- In the Visual Editor, add four text boxes, one for each number—days, hours, minutes, seconds—that will dynamically show the countdown values.
- In the Data tab > Code, click the Edit Content JS + CSS button. This opens the JavaScript and CSS editor for further customization.
- Within the
Builder.isBrowser
if
statement, locate the existing code and update thefinalDate
variable to the Unix timestamp of the desired countdown end date. For example, to countdown to New Year's Day 2030, setfinalDate
to1893456000000
(Unix timestamp of New Year's Day 2030).
The code should resemble this:
const second = 1000;
const minute = second * 60;
const hour = minute * 60;
const day = hour * 24;
state.days = "0";
state.hours = "0";
state.minutes = "0";
state.seconds = "0";
// New Year's Day 2030 - Unix time in milliseconds
let finalDate = 1893456000000;
const countDown = new Date(finalDate).getTime();
window.setInterval(myTimer, 1000);
function myTimer() {
let now = new Date().getTime();
let distance = countDown - now;
state.days = Math.floor(distance / day);
state.hours = Math.floor((distance % day) / hour);
state.minutes = Math.floor((distance % hour) / minute);
state.seconds = Math.floor((distance % minute) / second);
}
To get even more out of Templates with Builder, read Creating Templates.