javascript - 为动画文本输出编写脚本的问题
问题描述
我不明白问题是什么,我正在编写一个脚本,它必须自己输出文本,就好像有人在打字一样,但我不断抛出同样的错误,不要告诉我什么可以在这种情况下完成,我将不胜感激。
错误是子字符串方法不起作用 Uncaught TypeError: Cannot read properties of undefined (reading 'substring')
let quoteArray = [];
let index = 0;
let textPosition = 0;
let flag = true;
let destination = document.querySelector('#typedtext');
window.addEventListener('load', typewriter);
function loadQuote() {
const url = "https://api.quotable.io/random";
fetch(url)
.then(response => {
if(response.ok) {
return response.json();
} else {
console.log(response.status);
}
})
.then(data => {
quoteArray[index] = data.content;
});
}
function typewriter(){
if(flag) {
loadQuote();
quoteArray[index] + ' ';
flag = false;
}
destination.innerHTML = quoteArray[index].substring(0, textPosition); the error is here
if(textPosition++ != quoteArray[index].length){
setTimeout('typewriter()', 100);
} else {
quoteArray[index] = ' ';
setTimeout('typewriter()', 3000);
textPosition = 0;
flag = true;
}
}
解决方案
推荐阅读
- salesforce - 如何在闪电组件中将base64字符串显示为pdf
- c# - 尝试保存到数据库时,列数与第 1 行的值计数不匹配
- python - ipaddress 模块和 IPv6 反向指针
- string - 如何在 Flutter 的同一活动中将字符串值从一个类传递给另一个类?
- c# - 如何同时对我的 webapi 进行多个异步调用而不会出错?
- javascript - ZEIT 服务器无法读取 Multer 目标文件
- reactjs - 如何解决正则表达式拒绝服务等漏洞?
- r - 将行更改为列和分组依据
- python - 如何将我的子进程标准输出重定向到我的主程序 jupyter notebook?
- selenium - org.openqa.selenium.ElementNotVisibleException:单选按钮上的元素不可交互