You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
JavaScript
64 lines
1.7 KiB
JavaScript
9 years ago
|
define(function () {
|
||
|
var setRandomizedInterval = function (func, target, range) {
|
||
|
var timeout;
|
||
|
var again = function () {
|
||
|
timeout = setTimeout(function () {
|
||
|
again();
|
||
|
func();
|
||
|
}, target - (range / 2) + Math.random() * range);
|
||
|
};
|
||
|
again();
|
||
|
return {
|
||
|
cancel: function () {
|
||
|
if (timeout) {
|
||
|
clearTimeout(timeout);
|
||
|
timeout = undefined;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
|
||
9 years ago
|
var testInput = function (doc, el, offset, cb) {
|
||
9 years ago
|
var i = 0,
|
||
|
j = offset,
|
||
9 years ago
|
input = " The quick red fox jumps over the lazy brown dog.",
|
||
9 years ago
|
l = input.length,
|
||
|
errors = 0,
|
||
|
max_errors = 15,
|
||
|
interval;
|
||
|
var cancel = function () {
|
||
|
if (interval) { interval.cancel(); }
|
||
|
};
|
||
|
|
||
|
interval = setRandomizedInterval(function () {
|
||
|
cb();
|
||
|
try {
|
||
|
el.replaceData(j, 0, input.charAt(i));
|
||
|
} catch (err) {
|
||
|
errors++;
|
||
|
if (errors >= max_errors) {
|
||
|
console.log("Max error number exceeded");
|
||
|
cancel();
|
||
|
}
|
||
|
|
||
|
console.error(err);
|
||
|
var next = document.createTextNode("");
|
||
9 years ago
|
doc.appendChild(next);
|
||
9 years ago
|
el = next;
|
||
9 years ago
|
j = -1;
|
||
9 years ago
|
}
|
||
|
i = (i + 1) % l;
|
||
|
j++;
|
||
|
}, 200, 50);
|
||
|
|
||
|
return {
|
||
|
cancel: cancel
|
||
|
};
|
||
|
};
|
||
|
|
||
|
return {
|
||
|
testInput: testInput,
|
||
|
setRandomizedInterval: setRandomizedInterval
|
||
|
};
|
||
|
});
|