Bootcamp
Search…
📅
Course Schedule
0: Language and Tooling Teaching Guide
1: Frontend Basics Teaching Guide
2: Backend Basics Teaching Guide
3: Backend Applications Teaching Guide
4: Backend Structure Teaching Guide
5: Full-Stack Applications Teaching Guide
6: Frontend Infrastructure Teaching Guide
7: React Teaching Guide
8: Advanced React Teaching Guide
9: Advanced Topics Teaching Guide
🧮
Algorithms Teaching Guide
💼
Interview Prep Teaching Guide
☺
User Experience Teaching Guide
1.8: Timer

Introduction

Use setInterval to create a countdown timer.

Global Variables and Initialisation

1
let milliseconds = 5000;
2
const delayInMilliseconds = 1;
3
const output = document.createElement('div');
4
output.innerText = milliseconds;
5
document.body.appendChild(output);
Copied!

Timer Code

1
const ref = setInterval(() => {
2
output.innerText = milliseconds;
3
​
4
if (milliseconds <= 0) {
5
clearInterval(ref);
6
}
7
​
8
milliseconds -= 1;
9
}, delayInMilliseconds);
Copied!

Exercise

Create a timer that counts up instead of down. Have it stop after the same amount of time as the countdown timer above. (5000 milliseconds).
Last modified 1mo ago