Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'typerOrder' option with possible values: 'random', 'sequential' #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down
48 changes: 32 additions & 16 deletions src/jquery.typer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ String.prototype.rightChars = function(n){
typeDelay : 200,
clearOnHighlight : true,
typerDataAttr : 'data-typer-targets',
typerOrder : 'random',
typerInterval : 2000
},
highlight,
Expand All @@ -33,6 +34,7 @@ String.prototype.rightChars = function(n){
typeWithAttribute,
getHighlightInterval,
getTypeInterval,
intervalHandle,
typerInterval;

spanWithColor = function(color, backgroundColor) {
Expand Down Expand Up @@ -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 () {
Expand All @@ -186,7 +202,7 @@ String.prototype.rightChars = function(n){
}

typeWithAttribute($e);
setInterval(function () {
intervalHandle = setInterval(function () {
typeWithAttribute($e);
}, typerInterval());
});
Expand Down