From 9703db16ca1a88cf1f45f470624428838b0e3da3 Mon Sep 17 00:00:00 2001 From: gerard sychay Date: Wed, 4 Sep 2013 23:58:56 -0400 Subject: [PATCH] Add 'typerOrder' option with possible values: 'random', 'sequential' --- README.md | 1 + src/jquery.typer.js | 48 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3be0145..273e979 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ There are some options that are available to you as well: typeDelay : 200, clearOnHighlight : true, typerDataAttr : 'data-typer-targets', + typerOrder : 'random', // or 'sequential' typerInterval : 2000 } ``` diff --git a/src/jquery.typer.js b/src/jquery.typer.js index d1520cc..a6f0bc7 100644 --- a/src/jquery.typer.js +++ b/src/jquery.typer.js @@ -19,6 +19,7 @@ String.prototype.rightChars = function(n){ typeDelay : 200, clearOnHighlight : true, typerDataAttr : 'data-typer-targets', + typerOrder : 'random', typerInterval : 2000 }, highlight, @@ -33,6 +34,7 @@ String.prototype.rightChars = function(n){ typeWithAttribute, getHighlightInterval, getTypeInterval, + intervalHandle, typerInterval; spanWithColor = function(color, backgroundColor) { @@ -144,25 +146,39 @@ String.prototype.rightChars = function(n){ }, getHighlightInterval()); }; - typeWithAttribute = function ($e) { - var targets; + typeWithAttribute = (function () { + var last = 0; - if ($e.data('typing')) { - return; - } + return function($e) { + var targets; - try { - targets = JSON.parse($e.attr($.typer.options.typerDataAttr)).targets; - } catch (e) {} + if ($e.data('typing')) { + return; + } - if (typeof targets === "undefined") { - targets = $.map($e.attr($.typer.options.typerDataAttr).split(','), function (e) { - return $.trim(e); - }); - } + try { + targets = JSON.parse($e.attr($.typer.options.typerDataAttr)).targets; + } catch (e) {} - $e.typeTo(targets[Math.floor(Math.random()*targets.length)]); - }; + if (typeof targets === "undefined") { + targets = $.map($e.attr($.typer.options.typerDataAttr).split(','), function (e) { + return $.trim(e); + }); + } + + if ($.typer.options.typerOrder == 'random') { + $e.typeTo(targets[Math.floor(Math.random()*targets.length)]); + } + else if ($.typer.options.typerOrder == 'sequential') { + $e.typeTo(targets[last]); + last = (last < targets.length - 1) ? last + 1 : 0; + } + else { + console.error("Type order of '" + $.typer.options.typerOrder + "' not supported"); + clearInterval(intervalHandle); + } + } + })(); // Expose our options to the world. $.typer = (function () { @@ -186,7 +202,7 @@ String.prototype.rightChars = function(n){ } typeWithAttribute($e); - setInterval(function () { + intervalHandle = setInterval(function () { typeWithAttribute($e); }, typerInterval()); });