Im attempting to replicate a typing effect that cycles through an array and 'types out' each string. I've managed to get the effect working well, however I've found that if the current tagline is shorter than the next in the array it will go off its longer char length and will output the remaining missing characters from the next item.
I've probably explained poorly but if anyone feels like testing this out you'll see exactly what I mean.
I know that the issue is to do with where the tagCount is being incremented and where is is being used, but It's just one of those things I've been staring at for an hour and got nowhere.
var tagline = ['This is a tagline', 'Test string', 'Another test string', 'test'];
var tagCount = 0;
var output = document.getElementById('output');
var i = 0;
var countup = true;
var delayBool = true;
var interval = setInterval(function(){
if ((i < tagline[tagCount].length) && (countup == true)){
output.innerHTML += tagline[tagCount].charAt(i);
i++; }
else if((i > 0) && (countup == false)){
output.innerHTML = output.innerHTML.substring(0, output.innerHTML.length - 1);
i--; }
else if((i == 0) && (countup == false)){
countup = true; }
else if((i == tagline[tagCount].length) && (delayBool == true)){
delay();
delayBool = false;
}
console.log(tagCount)
}, 50);
function delay(){
if (tagCount < tagline.length-1){
tagCount++; }
else{
tagCount = 0; }
var delayInterval = setInterval(function(){
countup = false;
delayBool = true;
clearInterval(delayInterval);
}, 2000);
}
Heres the tiny bit of HTML
<h1 id=output></h1>