document.addEventListener('DOMContentLoaded',function(event){ // array with texts to type in typeWriter2 var dataText = [ "voice", "business", "community"]; // type one text in the typwriter // keeps calling itself until the text is finished function typeWriter2(text, i, fnCallback) { // chekc if text isn't finished yet if (i < (text.length)) { // add next character to h1 document.querySelector("i").innerHTML = text.substring(0, i+1) +''; // wait for a while and call this function again for next character setTimeout(function() { typeWriter2(text, i + 1, fnCallback) }, 100); } // text finished, call callback if there is a callback function else if (typeof fnCallback == 'function') { // call callback after timeout setTimeout(fnCallback, 2000); } } // start a typeWriter2 animation for a text in the dataText array function StartTextAnimation(i) { if (typeof dataText[i] == 'undefined'){ setTimeout(function() { StartTextAnimation(0); }, 3500); } // check if dataText[i] exists if (i < dataText[i].length) { // text exists! start typeWriter2 animation typeWriter2(dataText[i], 0, function(){ // after callback (and whole text has been animated), start next text StartTextAnimation(i + 1); }); } } // start the text animation StartTextAnimation(0); });