Countdown Timer
A simple countdown timer with start, stop, and restart functionality to display on your web app.
Last updated
Was this helpful?
A simple countdown timer with start, stop, and restart functionality to display on your web app.
Last updated
Was this helpful?
For the timer example, we add a progress bar along with 3 buttons to set, start and stop the timer. Additionally, we add an input field to enter the seconds the timer should be set to. Note that we give the progress bar the id="timer"
, so that we can reference it from our JavaScript code below.
The , the , and the are part of and are ready to use as they are.
For the timer to work, we'll need 3 global variables: The timerStartedAt
keeps track of what the initial time in seconds was when the timer was started. The variable timer
keeps track of the current remaining time in seconds and is decreased every second once the timer was started. The variable timerInterval
holds a reference to the automatic interval that executes every second. We need this reference to stop the interval when someone clicks "Stop".
To set the timer to a specific value, we connect the button to a function called setTimer()
, which is implemented as follows:
In line 3, we retrieve the value the user has entered. We interpret this value as duration in seconds the timer should count down. When the timer is set, we initialize the two variables timerStartedAt
and timer
(time left) with this value. No time has passed yet.
To clear any existing intervals if a timer is currently running, we call stopTimer()
in line 9.
In line 11, we set the progress bar to 100%, which makes it blue all the way. Since the timer is counting down, the progress bar will become smaller over time instead of bigger.
Finally, we log the information that the timer was set to the console (line 14).
To start the timer, we wire the startTimer()
function to the corresponding button. The function looks as follows:
The only thing that happens here is the initialization of a 1-second interval that executes the function decreaseTimer()
every second (1000 ms = 1 second). We store a reference to the interval on the variable timerInterval
.
The function decreaseTimer()
looks as follows and takes care of decreasing the timer and updating the progress bar:
We first decrease the timer by one in line 2. We then check whether the timer is still greater than zero. If not, we call stopTimer()
and timerDone()
. In the latter, we output a message to the console indicating the coffee is ready:
In line 12 of the decreaseTimer()
function, we transform the current progress of the timer to a scale between 0 and 100 because that is the number we need for the progress bar. Afterwards, we assign the new width of the progress bar, so that we get a visual update.
To pause the timer, the user can click on "Stop" at any time. To resume the user can click "Start" again.