控制台在第5行和第8行显示错误。错误是“Uncaught insert function argument不是字符串”。任何帮助将不胜感激。谢谢!
$(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;
}
}
});
});
答案 0 :(得分:2)
当message[da++]
“不是字符串”时,有三种情况:
message
是空字符串.charAt()
方法da == message.length
- 仅当da
已经大于长度时才会结束。然而,指数从零开始,从0
到length-1
。要解决此问题,请使用
// 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);