Names Sequencing

0

I'm trying to set up a javascript that sequentially writes a phrase within my div, and after a few seconds it replaces the text with another pre-programmed one, I already tried to do with for, timer and still can not, instead of waiting the first message is written to write the second it 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>
    
asked by anonymous 31.05.2017 / 03:49

1 answer

1

In the setTimeout call the write function inside as in the example below and the first call does not need timeout, Soon will call it directly, and after 3 seconds it will call the second method:

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);
}
escrever(texto[0], div);
setTimeout(function(){ escrever(texto[1], div)}, 3000);
<div id="log"></div>
    
31.05.2017 / 04:42