Generate a countdown

Write a function that generates a countdown, with user inputs to determine where to start the countdown from, the speed of the countdown and the message to display once complete.


countdown

A countdown is a timer that counts down from a specified number to zero, often used in applications such as games, event launches, or reminders.

A countdown typically involves decrementing a time variable until it reaches zero, at which point you can trigger an event or action.


Example ...

Number to start countdown from ...

10 countdown from 10 to 0

Speed of coundown ...

1000 delay of 1000 ms (1 second)

Message to display when coundown reaches 0 ...

"Go!!"

Print the countdown to the console.

10 → printed to console after 1000 ms

9 → printed to console after 1000 ms

8 → printed to console after 1000 ms

7 → printed to console after 1000 ms

6 → printed to console after 1000 ms

5 → printed to console after 1000 ms

4 → printed to console after 1000 ms

3 → printed to console after 1000 ms

2 → printed to console after 1000 ms

1 → printed to console after 1000 ms

0 → printed to console after 1000 ms

Go!! → printed to console


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Generate a countdown using ↴

setInterval() → method repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

clearInterval() → method cancels a timed, repeating action which was previously established by a call to setInterval().


setInterval() method repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. It continues to run at the specified intervals until it is explicitly stopped.

syntax

setInterval(function, delay);

function → function containing a block of code

delay → time interval between the execution of the function

Print to console the countdown from 3.

let count = 3; → count

const interval = setInterval(() => {

console.log(count); → print to console

count--; → decrement count by 1

}, 1000); → delay in milliseconds

returns ↴

3 → printed to console after 1000 ms

2 → printed to console after 1000 ms

1 → printed to console after 1000 ms

0 → printed to console after 1000 ms

-1 → printed to console after 1000 ms

-2 → printed to console after 1000 ms

... continues to run until it is explicitly stopped.

The return value of the function is an intervalID which can be used later to clear the interval if needed.


clearInterval() method cancels a timed, repeating action which was previously established by a call to setInterval()

If the parameter provided does not identify a previously established action, this method does nothing.

syntax

clearInterval(intervalID);

intervalID → identifier of the repeated action you want to cancel. This ID was returned by the corresponding call to setInterval()

Print to console the countdown from 3 to 0 and print the message "Go".

function countdown() {

let count = 3; → start countdown from 3

const interval = setInterval(() => {

console.log(count);

count--;

if (count < 0) { → end countdown at 0

clearInterval(interval);

console.log("Go"); → message

}

}, 1000); → delay in milliseconds

}

call function

countdown(); returns ↴

3 → printed to console after 1000 ms

2 → printed to console after 1000 ms

1 → printed to console after 1000 ms

0 → printed to console after 1000 ms

Go → printed to console


Initialize a variable to hold the number to start countdown from.

const countFrom = 5; → user input

Initialize a variable to hold the delay interval.

const delay = 500; → user input

Initialize a variable to hold the message to display.

const message = "BLAST OFF"; → user input


Define a function countdown to countdown to zero from a given number, at a given interval duration and a message to display.

function countdown() {}

The function takes no input, but has user defined variables.

Starting count for the countdown.

const countFrom = 5 countFrom

Interval duration in milliseconds.

const delay = 500 delay

Message to display after countdown.

const message = "BLAST OFF" message

Initialize count with the starting value.

let count = countFrom count

Set an interval to execute the countdown logic.

const interval = setInterval(() => { interval intervalID

Log the current count to the console.

console.log(count)

Decrement the count by 1

count--

Check if the count has reached below 0

if (count < 0) {

If true, clear the interval to stop the countdown.

clearInterval(interval)

Log the final message.

console.log(message)

Execute every 500 milliseconds.

}, delay)


Call the function with ↴

countdown();


Generate a countdown.

const countFrom = 5;

const delay = 500;

const message = "BLAST OFF";

function countdown() {

let count = countFrom;

const interval = setInterval(() => {

console.log(count);

count--;

if (count < 0) {

clearInterval(interval);

console.log(message);

}

}, delay);

}

call function

countdown(); returns ↴

5 → printed to console after 500 ms

4 → printed to console after 500 ms

3 → printed to console after 500 ms

2 → printed to console after 500 ms

1 → printed to console after 500 ms

0 → printed to console after 500 ms

BLAST OFF → printed to console

Generate a countdown

number to start countdown from
interval (milliseconds)