嵌套javascript函数超时

时间:2016-03-04 12:31:06

标签: javascript jquery

我正在编写一个javascript函数(实际上是一个jQuery原型)来在元素中键入一些文本。我正在使用this function并将其包装成jQuery原型,如下所示:

$.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);
    }());
}

它给了我这个错误:

  

jquery.min.js:3未捕获的TypeError:无法读取未定义的属性'createDocumentFragment'

我的猜测是它在第二次超时调用时找不到typeIt函数,有人可以帮我解决这个问题吗?我想在jQuery原型函数中保留所有代码。

0 个答案:

没有答案