I'm writing a javascript function (actually a jQuery prototype) to type some text inside an element. I'm taking this function and wrapping it into a jQuery prototype, like this:
$.fn.typeText = function(text) {
var txtLen = text.length,
timeOut,
char = 0;
$(this).text('|');
(function typeIt() {
var humanize = Math.round(Math.random() * (200 - 30)) + 30;
timeOut = setTimeout(function() {
char++;
var type = text.substring(0, char);
$(this).text(type + '|');
typeIt();
if (char == txtLen) {
$(this).text($(this).text().slice(0, -1)); // remove the '|'
clearTimeout(timeOut);
}
}, humanize);
}());
}
It's giving me this error:
jquery.min.js:3 Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined
My guess is that it can't find the typeIt
function on the second timeout call, can someone help me out in this? I want to keep all code for this inside the jQuery prototype function.