我试图实现一个向用户“输入”消息的功能。像“T” - 等待 - “Th” - 等待 - “Tha” ...我想出了这个功能,但它会等待然后立即更新所有字母(而不是单独更新):
var tu = 'Thank you'
var timing = 1000
for (var i=0; i<=tu.length; i++) {
setTimeout(function (){input.text(tu.slice(0, i))}, timing)
timing = timing + 1000
}
但是当我这样做时(不要笑),它起作用了..
setTimeout(function (){input.text('t')}, 400)
setTimeout(function (){input.text('th')}, 800)
setTimeout(function (){input.text('tha')}, 3000)
setTimeout(function (){input.text('than')}, 4000)
setTimeout(function (){input.text('thank')}, 5000)
setTimeout(function (){input.text('thank ')}, 6000)
setTimeout(function (){input.text('thank y')}, 7000)
setTimeout(function (){input.text('thank yo')}, 8000)
setTimeout(function (){input.text('thank you')}, 9000)
任何人都可以阐明为什么循环的行为与我的剪切粘贴工作不同吗?