var delayTime = 2000;
for(var i = 0; i<this.textToWrite.length; i++){
setTimeout(
(
function (s){
return function () {
this.writeText += s;
console.log(this.writeText);
}
}(this.textToWrite[i])
), delayTime)
delayTime += 2000;
}
this.writeText 有问题。它是全局变量,但当我甚至评论 + = s 行时,我有未定义的值...(我设置writeText:string =“”globaly)是 this.writeText 参考到全局变量?如何在此示例中访问全局变量? 我需要将textToWrite对象的char配置为writeText,延迟时间为2s。
答案 0 :(得分:0)
问题可能是您使用了功能词。
setTimeout(
((s) => {
return () => {
this.writeText += s;
console.log(this.writeText);
}
}(this.textToWrite[i])
), delayTime)
另一种方法是使用它而不使用它的全局变量:
setTimeout(
((s) => {
return () => {
writeText += s;
console.log(this.writeText);
}
}(this.textToWrite[i])
), delayTime)
答案 1 :(得分:0)
仅使用箭头功能
settimeout(x => {
console.log(x);
}, 1000);
不喜欢这样,它不会接受辅助参数
settimeout(funtion(x) {
console.log(x);
}, 1000);