Junior — Senior
Changing element text using setTimeout inside an infinite loop
livecode
Task condition
It is necessary to determine how the page will behave when an infinite while loop is launched inside the DOMContentLoaded event handler, within which a deferred setTimeout call is set on each iteration. This timer updates the content of an element on the page.
<html>
<head>
<title>JS test 2</title>
</head>
<body>
<div id="container">0</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const hostEl = document.getElementById('container');
let counter = 0;
while (true) {
setTimeout(() => {
hostEl.innerText = counter++;
}, 10);
}
});
</script>
</body>
</html>