我想在 while 循环中重复一段文本 2 秒。如何在 2 秒后打破循环?
这是我迄今为止尝试过但不起作用的方法:
var repeat = true;
setTimeout(function() { var repeat = false }, 2000)
while(repeat) {
console.log("Let's repeat for 2 seconds...");
}
最佳答案
除了其他答案之外,您还可以检查时间:
const start = +new Date;
while(+new Date < start + 2000) {
console.log("Let's repeat for 2 seconds...");
}
关于javascript - 2 秒后离开循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439744/