franhaselden
6/8/2015 - 1:57 PM

Text typer - launches on a trigger (can be anything) and types out the text with mistakes. http://codepen.io/franhaselden/pen/jbWoGJ

Text typer - launches on a trigger (can be anything) and types out the text with mistakes. http://codepen.io/franhaselden/pen/jbWoGJ

.typer {
    display:none;
}
<p>Click here to start it</p>
<br />

<h2 id="typer-get-in-touch" class="typer" data-text="Get in Touhc^^ch"></h2>
/* The trigger function */
$('p').click(function () {
    var outputID = $("#typer-get-in-touch");
    textTyping(outputID);
});


/* The typer function */
function textTyping(outputID) {
    var textString = $(outputID).data("text");
    if (textString === "") return;
    $(outputID).show();
    var textArray = textString.split("");
    var texttypeing = setInterval(

    function () {
        typeOutText(outputID, textArray);
    }, 170);

    function typeOutText(outputID, textArray) {
        if (textArray[0] == "^") {
            outputID.text(function (index, text) {
                return text.replace(/(\s+)?.$/, '');
            });
            textArray.shift();
        } else {
            if (textArray.length > 0) {
                outputID.append(textArray.shift());
            } else {
                $(outputID).data("text", "");
                clearTimeout(texttypeing);
            }
        }
    }
}