我一直试图让它通过所有的for循环,但它总是说数组。。text每当我尝试获取数组中的任何元素时,行/消息都是未定义的。
我想得到整个对象,并使用split("(从文本中得到每个字母。每当我查看控制台日志,试图从数组中获取任何内容(甚至其长度(时,都会说它是未定义的,但每当我将数组放入其中时,它都会正常显示对象数组。如何使定义数组中的元素(对象(??
function say(messages = [{text: ' ', speed: 90, pause: false, colour: ['RED//BLUE']}]){
let alphabets = [];
let textLines = messages
console.log(textLines.length)
textLines.forEach((line, i) => {
if (i < textLines.length - 1) line.text += " ";
console.log(typeof textLines[1])
line.text.split("").forEach((alphabet) => {
let span = document.createElement("span");
span.textContent = alphabet;
textContainer.appendChild(span);
alphabets.push({
span: span,
isSpace: alphabet === " " && !line.pause,
delayAfter: line.speed,
classes: line.classes || []
});
});
});
function revealLetter(list) {
let nextLetter = list.splice(0, 1)[0];
nextLetter.span.classList.add("revealed");
nextLetter.classes.forEach((c) => {
nextLetter.span.classList.add(c);
});
let delay = nextLetter.isSpace && !nextLetter.pause ? 0 : nextLetter.delayAfter;
if (list.length > 0) {
setTimeout(function () {
revealLetter(list);
}, delay); // the delay is the talking speeds
}
}
setTimeout(() => { // to start the loop
revealLetter(alphabets);
}, 600)
}
say({ // can change what's said, the colour of text, the speed its displayed and if there's a pause (4 things)
messages: [
{text: 'Literally what does that even mean???',
speed: talkingSpeeds.fast,
pause: false,
},
{text: 'Like. what?',
speed: talkingSpeeds.normal,
pause: true
},
{text: 'Stop talking.',
speed: talkingSpeeds.slow,
pause: true,
colour: ['red']
}
]
})
这个问题有两种解决方案:
-
或者你可以在say函数中发送数组,比如:
比如([…](
-
或者你可以在say函数中更改你接受的参数,比如:
函数say({messages=[{text:",速度:90,暂停:false,颜色:[‘RED//BLUE’]}]}({…}