JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>
<div class="jumbotron">
<div class="container">
<blockquote>
<span id="target"></span>
<span id="cursor"></span> <!-- used for the blinking cursor effect -->
</blockquote>
</div>
</div>
CSS
@import url('http://getbootstrap.com/dist/css/bootstrap.css');
JavaScript
// Credits to Yoshi: http://stackoverflow.com/a/13325547/119624
(function ($) {
// writes the string
//
// @param jQuery $target
// @param String str
// @param Numeric cursor
// @param Numeric delay
// @param Function cb
// @return void
function typeString($target, str, cursor, delay, cb) {
$target.html(function (_, html) {
return html + str[cursor];
});
if (cursor < str.length - 1) {
setTimeout(function () {
typeString($target, str, cursor + 1, delay, cb);
}, delay);
}
else {
cb();
}
}
// clears the string
//
// @param jQuery $target
// @param Numeric delay
// @param Function cb
// @return void
function deleteString($target, delay, cb) {
var length;
$target.html(function (_, html) {
return html + "<br/>";
});
cb();
}
// jQuery hook
$.fn.extend({
teletype: function (opts) {
var settings = $.extend({}, $.teletype.defaults, opts);
return $(this).each(function () {
(function loop($tar, idx) {
// type
typeString($tar, settings.text[idx], 0, settings.delay, function () {
// delete
setTimeout(function () {
deleteString($tar, settings.delay, function () {
if(idx+1 < settings.text.length)
loop($tar, (idx + 1) % settings.text.length);
});
}, settings.pause);
});
}($(this), 0));
});
}
});
// plugin defaults
$.extend({
teletype: {
defaults: {
delay: 100,
pause: 5000,
text: []
}
}
});
}(jQuery));
$('#target').teletype({
text: [
'You have spent your days creating puzzles for others. ',
'Now it\'s time for me to provide a puzzle for you. ',
'The timer has started, prove your worth. ',
'Would you like to play a game?'
],
pause:20
});