问题描述:

The console is showing errors on lines 5 and 8. The error is "Uncaught insert function argument is not a string." Any help would be greatly appreciated. Thanks!

$(function() {

var animation = false;

function typed(term, message, delay, finish) {

animation = true;

var da = 0;

term.set_prompt('');

var interval = setInterval(function() {

term.insert(message[da++]);

if(da > message.length) {

clearInterval(interval);

setTimeout(function() {

term.set_command('')

term.set_prompt(message + ' ');

animation = false;

finish && finish();

}, delay);

}

}, delay);

}

$('#fyeah').terminal(function(cmd, term) {

var finish = false;

}, {

name: 'test',

greetings: null,

onInit: function(term) {

var msg = "testing";

typed(term, msg, 150, function() {

finish = true;

});

},

keydown: function(e) {

if (animation) {

return false;

}

}

});

});

网友答案:

There are three cases when message[da++] "is not a string":

  • message is the empty string
  • (old) IE do not support bracket notation for getting single characters of strings, better use the .charAt() method
  • in the last iteration of your loop, da == message.length - it ends only when da is already greater than the length. Yet, the indices are zero-based and run from 0 to length-1.

To fix it, use

// init
var da = 0;
var interval = setInterval(function() {
    if (da < message.length) {
        term.insert(message.charAt(da++)); // maybe better move the incrementing
                                           // out, to the end of the loop
    } else {
        clearInterval(interval);
        // teardown / callback
    }
}, delay);
相关阅读:
Top