I'm trying to get a timer to type text, delete it, and repeat, cycling through an array of header titles. I can get it to write once, but my limited understanding of timers is hindering me. Here's what I have so far. Before I put the write logic in it's own method, I could call the writeText() plugin manually from the console, but now it's only working once. Here's a link to a codepen
<!-- HTML -->
<div class="holder">
<h1><span>Lorem ipsum dolor sit amet, consectetur adipisicing </span><img id="cursor" src="https://copy.com/oBelwqiCr3Fa"></h1>
</div>
// Javascript
// counter and array of text to type in the header
var headerArray = [
"The Internet can't teach you about marketing",
"I wish I learned in art school",
"Every Successful Startup Eventually Does"
],
headerCounter = 0,
// headerContainer = $('.holder h1 span'),
headerContainer = document.querySelector('#cursor').previousElementSibling;
// function to type the text
function typeText(i) {
$('.holder h1 span').text('').writeText(headerArray[i]);
headerCounter++;
if (headerCounter >= headerArray.length) {
headerCounter = 0;
}
}
$(function() {
// fades cursor in and out
setInterval(function () {
$('#cursor').fadeToggle(400);
}, 400);
// calls function to type the text
setInterval(typeText(headerCounter), 5000);
});
// plugin that writes out the text like a typewriter
(function($) {
$.fn.writeText = function(content) {
var contentArray = content.split(""),
current = 0,
elem = this;
setInterval(function() {
if(current < contentArray.length) {
elem.text(elem.text() + contentArray[current++]);
}
}, 50);
};
})(jQuery);
// plugin that deletes text
(function($) {
$.fn.deleteText = function(content) {
var newContent = content.slice(0, content.length - 1),
current = 0,
elem = this;
setInterval(function() {
if (elem.text().length == 0) {
elem.text(newContent);
current--;
}
}, 50);
};
})(jQuery);