Simple text ticker

by mrjordy

HTML

<div id="foo"></div>

JavaScript

$.fn.typer = function(text, options){
    options = $.extend({}, {
        char: ' ',
        delay: 1000,
        duration: 600,
        endless: true
    }, options || text);

    text = $.isPlainObject(text) ? options.text : text;

    var elem = $(this),
        isTag = false,
        c = 0;
    
    (function typetext(i) {
        var e = ({string:1, number:1}[typeof text] ? text : text[i]) + options.char,
            char = e.substr(c++, 1);

        if( char === '<' ){ isTag = true; }
        if( char === '>' ){ isTag = false; }
        elem.html(e.substr(0, c));
        if(c <= e.length){
            if( isTag ){
                typetext(i);
            } else {
                setTimeout(typetext, options.duration/10, i);
            }
        } else {
            c = 0;
            i++;
            
            if (i === text.length && !options.endless) {
                return;
            } else if (i === text.length) {
                i = 0;
            }
            setTimeout(typetext, options.delay, i);
        }
    })(0);
};

$('#foo').typer(["	We are not simply motivated, but engaged, and fully immersed in the success of this operation. We are not asked to do this. It is not expected of us. We do this because we care, deeply, as a direct result of how much you care about us.	",
"	Managers genuinely care about employees. Excellent pay. Work is sometimes fast-paced but as long as you contribute, there is no micro-management. The staff is generally more experienced, with many having a former Military background.	",
"	Hard-working and helpful co-workers, salary is excellent and the work is challenging.	",
"	Falconwood is an awesome company, with really sharp people. They pay well, and you feel very much part of the team.	",
"	Uses a model to keep overhead low (no extravagance) which allows the to make competitive bids while still providing competitive salaries.	",
"	The pay is great. Everyone I've met with at the company is really awesome. It feels...