forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (29 loc) · 1.36 KB
/
index.js
File metadata and controls
33 lines (29 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/2-Browsers/Week1#exercise-4-whats-the-time
1. Inside the `index.js`, complete the `addCurrentTime` to add the current time
to the webpage. Make sure it's written in the HH:MM:SS notation (hour, minute,
second). Use `setInterval()` to make sure the time stays current.
2. Have the function execute when it's loading in the browser.
------------------------------------------------------------------------------*/
function addCurrentTime() {
// Create or select a container for the time
let timeContainer = document.getElementById('time');
if (!timeContainer) {
timeContainer = document.createElement('h1');
timeContainer.id = 'time';
document.body.appendChild(timeContainer);
}
// Function to update the time
function updateTime() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
timeContainer.textContent = `${hours}:${minutes}:${seconds}`;
}
// Update immediately and then every second
updateTime();
setInterval(updateTime, 1000);
}
// Execute when the browser has loaded
window.addEventListener('load', addCurrentTime);