0
I’m trying to assemble a javascript that sequenced a sentence inside my div, and after a few seconds it replaces the text with another pre-programmed, I’ve tried to do with for, timer and still can’t, instead of waiting for the first message to be written to write the second one he simply writes both at the same time.
var div = document.getElementById('log');
var texto = ['Hoje está um lindo dia!','Hoje não está um lindo dia!','Hoje o dia estar horrivel'];
function escrever(str, el) {
var char = str.split('').reverse();
var typer = setInterval(function () {
if (!char.length) return clearInterval(typer);
var next = char.pop();
el.innerHTML += next;
}, 100);
}
setTimeout(escrever(texto[0], div),5000);
setTimeout(escrever(texto[1], div),5000);
<div id="log"></div>
But you’re writing both at the same time, with the same timeout...
– Aline
Ah, besides the fact that innerhtml will add the desired content, not replace the whole content, which seems to me to be its goal.
– Aline
You put the timeout equal, Change for example setTimeout(write(text[0], div),1000); and in the second leaves the 5000, In 1 sec will execute 1 and after 4 seconds the other
– Edenilson Bila