I'm using the typewriter effect from this answer on SO with the intention of having the first div type out, followed by the second div (when the first div has finished typing out).
<div class="text">
Lorem ipsum dolor sit amet...
</div>
<div class="text">
Cras eros augue, tempor...
</div>
JS:
// Create typewrite function as jQuery plugin:
// (From https://stackoverflow.com/a/22266737/3217306)
$.fn.typeText = function() {
var keystrokeDelay = 10,
self = this,
str = self.html(),
i = 0,
isTag,
text;
self.html("");
(function type() {
text = str.slice(0, ++i);
if (text == str) {
console.log("completed typing");
return;
}
self.html(text);
var char = text.slice(-1);
if (char == "<") {
isTag = true;
}
if (char == ">") {
isTag = false;
}
if (isTag) {
return type();
}
setTimeout(type, keystrokeDelay);
} ());
};
var textElements = $(".text");
// For each div.text, type them out
textElements.each(function() {
$(this).typeText();
});
The trouble is, both type out at the same time. I've tried so many different things involving promises, callbacks etc. but I can't get them to work for this situation.