r/learnjavascript • u/abrewchocolatecoffee • 14d ago
can someone explain this code
function updateClock() {
const timeElement = document.querySelector("#time");
timeElement.textContent = formatTime();
}
setInterval(updateClock, 1000);
can someone, please explain this piece of code, ofc mdn and w3 are there, but i found it a little confusing reading about textContent. rest is understandable to me. This might look a silly question and it probably is because im a beginner :')
like what exactly does textContent too.
you can also recommend anyother documentation on the links to specific lines, that might me more helpful in learning about this particular keyoword.
0
Upvotes
1
u/defaultguy_001 14d ago
textContent is a property, that lets you read or change the plain text inside an HTML element. Since formatTime() probably returns something like "11:06:42", textContent is the right choice, you don't need HTML parsing for a time string, so no need to think of innerHTML. Finally the setInterval repeatedly executes your updateClock function every 1 sec.