Orientado a objetos:
https://jsfiddle.net/hct6q5ce/
Código HTML:
Ver originalvar rotor = new Rotor ({
id: 'rotando',
interval: 1000,
frases: [
"TEXTO FRASE 0
<br>-AUTOR 0",
"TEXTO FRASE 1
<br>-AUTOR 1",
"TEXTO FRASE 2
<br>-AUTOR 2"
]
});
rotor.play();
Código Javascript
:
Ver originalfunction Rotor(options) {
if (!(this instanceof Rotor)) {
return new Rotor(options)
}
this.frases = options.frases
this.elem = document.getElementById(options.id) //|| options.elem
this.interval = options.interval
this.indice = Math.floor(Math.random() * this.frases.length)
}
Rotor.prototype.play = function () {
if (this.indice === this.frases.length) {
this.indice = 0
}
this.elem.innerHTML = this.frases[this.indice]
console.log(this.indice)
++this.indice
setTimeout(this.play.bind(this), this.interval)
}