Мне нужен следующий код для ввода / нетипизации каждой строки, по одной в том порядке, в котором они перечислены. В настоящее время он выполняется, но i ++ запускает слишком много раз в цикле и нарушает порядок событий. Помогите исправить итерации, чтобы любое количество строк можно было набирать / не печатать по порядку.
<div class="flex-container">
<h1>Innovative Solutions
<br>for
<span id="str"></span>
</h1>
<hr>
<p>This is filler content. The text in this area will be replaced when copy for the site becomes available. This is filler content. The text in this area will be replaced when copy for the site becomes available.</p>
<a href="#">Learn More</a>
</div>
$(function() {
var speed = 200;
var speed2 = 50;
var str = document.getElementById('str');
var i = 0;
var isRemoving = false;
var messages = [
"Cyber Security...",
"Vulnerability Assessments...",
"Program Management...",
"Compliance Management..."
]
function action() {
console.log('Action')
if (isRemoving) {
if (str.innerText.length > 0) {
str.innerText = str.innerText.substr(0, str.innerHTML.length - 1);
setTimeout( action, speed2 );
return;
}
isRemoving = false;
i++;
if (i === messages.length) {
i = 0;
}
setTimeout( action, 500 );
return;
}
var message = messages[i];
str.innerText = message.substr(0, str.innerHTML.length + 1);
if (str.innerText.length === message.length) {
setTimeout(function() {
isRemoving = true;
console.log(isRemoving)
}, 2000)
}
setTimeout( action, isRemoving ? speed2 : speed );
}
setTimeout( action, speed ) ;
})