0
I have a little problem here, I hope someone can help me.
I have two elements: h1
and h2
, I made a script for typing effect for both, only I want the typing effect of h2
is started only when the h1
is finished, or after a while I select. I tried to do it this way but it didn’t work:
<script>
function typeWrite (elemento) {
const textoArray = elemento.innerHTML.split('');
elemento.innerHTML = '';
textoArray.forEach((letra, i) => {
setTimeout(function() {
elemento.innerHTML += letra
}, 75 * i)
});
}
const titulo = document.querySelector('h1')
typeWrite(titulo);
</script>
<script>
var segundos = 4;
setTimeout ( function typeWrite (elemento) {
const textoArray = elemento.innerHTML.split('');
elemento.innerHTML = '';
textoArray.forEach((letra, i) => {
setTimeout(function() {
elemento.innerHTML += letra
}, 75 * i)
});
}, segundos * 1000);
const titulo2 = document.querySelector('h2')
typeWrite(titulo2);
</script>
Can someone tell me what the mistake is? Thank you!
I have already posted a suggestion but here is an answer to your question: the mistake is that in
setTimeout
of the 4 seconds you are redeclaring the functiontypeWrite
and not calling her. The right thing would be something likesetTimeout(()=>{
 const titulo2 = document.querySelector('h2')
 typeWrite(titulo2);
 }, segundos * 1000);
– José Guilherme Oliveira