I had an approach to add a typewriter effect to my text using Javascript. I used the following snippet to do so :
const text = document.querySelector('.type')
const content = text.textContent
let counter = 0
text.textContent = ''
while (counter < content.length) {
setTimeout(function () {
text.textContent += content[counter];
counter += 1
}, 1000)
}
I know that this approach seems ridiculous , but I'm curious why it does this. When I try to run this snippet, the browser (Chrome) becomes not responding. I want to know if this code generates an infinite loop, and why? And if someone can provide me some alternative way to get the wanted result.