I would like to display a counter on a web page who display all numbers one by one
I wrote this code, but it doesn’t work, it displays only the last number
Can you help me? ChatGPT was useless :))
<!DOCTYPE html>
<html>
<head>
<title>Compteur de 1 à 1000000</title>
</head>
<body>
<div id="counter"></div>
<script>
const counterElement = document.getElementById('counter');
for (let i = 1; i <= 1000000; i++) {
counterElement.innerHTML = i;
}
</script>
</body>
</html>
2
Answers
The easiest way would be to make your code
async
by wrapping it in such a function and then adding a delay inside yourfor
loop, like this:It is likely happening too fast for you to discern the counter. If you include a setTimeout (or something similar) you will see the numbers count up. Perhaps something like this: