Javascript while 在控制台中不起作用

标签 javascript while-loop console timestamp javascript-debugger

早上好,当我想在控制台中运行此代码时,浏览器阻止了我(错误:网页正在减慢您的浏览器速度)。 我不确定如何避免此错误。

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/

相关文章:

python导入名称文件并按字母顺序排序

linux - 在Linux中,如何独占console std out?

javascript - 无法在 Internet Explorer 10 上加载 Google Cloud Endpoints

javascript - 如何使用 Promise.all 避免 promise 构造函数反模式

while 循环 JOIN 中的 PHP SQL 查询

Python 3.3.5 控制台无法在 PyCharm 2016.3.1 中启动

linux - 如何检查我是否可以通过 SSH 访问 Unix 环境中的 JBoss Web 控制台

javascript - 尝试将参数从 View 传递到 Controller 方法

javascript - 如何在 Angular 7 应用程序中构建目录选择器?

c - 打印两个 double 的答案给出答案 0.00