3 回答
TA贡献1825条经验 获得超6个赞
实现这一目标的最简洁方法是使用asyncand await:
async function yazmaeffetkti() {
// ...
await new Promise(resolve => setTimeout(resolve, 3000));
console.log(currentcumle);
// ...
}
TA贡献1802条经验 获得超10个赞
除了Robo Robok 的回答之外,这里是一个使用相同概念但使用then代替的示例async/await。我个人更喜欢它,但这取决于你。
function yazmaeffetkti() {
var cumleler = ["salam necesen", "yaxsiyam sen "]
currentcumle = ""
for (var i = 0; i < cumleler.length; i++) {
cumle = cumleler[i]
for (var z = 0; z < cumle.length; z++) {
currentcumle += cumle[z]
new Promise(resolve => setTimeout(resolve, 3000)).then(() => {
console.log(currentcumle);
});
}
currentcumle = ""
}
}
yazmaeffetkti()
TA贡献2039条经验 获得超7个赞
这是我的版本:
async function yazmaeffetkti() {
var cumleler = ["salam necesen", "yaxsiyam sen "]
currentcumle = ""
for (var i = 0; i < cumleler.length; i++) {
cumle = cumleler[i]
for (var z = 0; z < cumle.length; z++) {
currentcumle += cumle[z]
await stateChange(currentcumle)
}
currentcumle = ""
}
}
var stateChange = (currentcumle) => new Promise((resolve)=>{
setTimeout(function() {
console.log(currentcumle);
resolve(true);
}, 1000);
});
yazmaeffetkti()
添加回答
举报