早上好,当我想在控制台中运行此代码时,浏览器阻止了我(错误:网页正在减慢您的浏览器速度)。 我不确定如何避免此错误。
var term = Date.now();
var term = term + 30000; // + 30 sec
while (1) {
var now = Date.now();
if(now > term) {
alert("Now");
break; // when is actual time (now) bigger than term, I will stop the script
}
}
最佳答案
以非阻塞形式编写。使用setTimeout:
setTimeout(()=> {
alert("Now");
}, 30000);
如果你想等待特定时间戳只需 setTimeout X 秒,执行操作:
var miliseconds_to_wait = timestamp - Date.now();
setTimeout(()=>{
alert("Now");
}, miliseconds_to_wait);
关于Javascript while 在控制台中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398242/