我想在 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...");
}