Displaying Time on a Webpage.
function updateTime() {
const timeElement = document.getElementById('time')
const now = new Date()
timeElement.textContent = now.toLocaleTimeString()
}
updateTime()
setInterval(updateTime, 1000)
function updateTime() {
const timeElement = document.getElementById('time')
const now = new Date()
timeElement.textContent = now.toLocaleTimeString()
}
updateTime()
setInterval(updateTime, 1000)
Display Time
JavaScript makes it simple to display dynamic content on a webpage, such as the current time. In this snippet, we use the Date object and the toLocaleTimeString method to fetch and display the current time. By combining this with setInterval, the time updates automatically every second.
-EG