Created
December 22, 2016 15:40
-
-
Save danman01/e9e422ec7318063f900267900c8aa730 to your computer and use it in GitHub Desktop.
testing some javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<h1>heyy</h1> | |
<type></type> | |
<script src="script.js" type="text/javascript" charset="utf-8"></script> | |
</body> | |
</html> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded',function(event){ | |
// array with texts to type in typewriter | |
var dataText = [ "More Traffic.", "More Leads.", "More Customers.", "More Referrals.", "Faster Growth." ]; | |
// type one text in the typewriter | |
// keeps calling itself until the text is finished | |
function typeWriter(text, i, fnCallback) { | |
// check if text isn't finished yet | |
if (i < (text.length)) { | |
// add next character to h1 | |
document.querySelector("type").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>'; | |
// wait for a while and call this function again for next character | |
setTimeout(function() { | |
typeWriter(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, 700); | |
} | |
} | |
// start a typewriter animation for a text in the dataText array | |
function StartTextAnimation(i) { | |
if (typeof dataText[i] == 'undefined'){ | |
setTimeout(function() { | |
StartTextAnimation(0); | |
}, 20000); | |
} | |
// check if dataText[i] exists | |
if (dataText[i] != undefined && i < dataText[i].length) { | |
// text exists! start typewriter animation | |
typeWriter(dataText[i], 0, function(){ | |
// after callback (and whole text has been animated), start next text | |
StartTextAnimation(i + 1); | |
}); | |
} | |
} | |
// start the text animation | |
StartTextAnimation(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment