javascript - setTimeout 函数比应有的速度要快

标签 javascript node.js selenium webstorm

我在 selenium 测试中有以下函数,并且 setTimeout 函数总是比应有的速度快 25% 左右。在本例中,需要等待 20 秒,函数在 15 秒后完成。

test.describe('basic login test',function(){
    this.timeout(timeout);
    // variables
    test.before(...);

    test.it.only('Test', function(done){
        testLoginPage.load().then(...)
        .then(...).then(...)
        .then(...).then(...)
        .then(function(){
            var first = "1st: " + new Date().getTime();
            console.log(first);    
            setTimeout(function(){
                driver.getTitle().then(function(title){
                    assert.equal(title, 'Tittle', 'Error.');
                });
                console.log(first);
                console.log("2nd: " + new Date().getTime());
                done();
            }, 20000);
       }).then(...)
    });
    test.after();
});

输出:

1st: 1457706590459
1st: 1457706590459
2nd: 1457706605462

最佳答案

不可能,但以下情况除外。

  • 您正在使用的代码或库具有覆盖的自定义 setTimeout函数,它正在调用实际 setTimeout函数运行时间更短。

  • 您正在使用 JavaScript运行时(非常旧),存在非常非常严重问题。

  • 其他一些function正在中间打印它(不过看起来不像您的代码)

关于javascript - setTimeout 函数比应有的速度要快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941946/

相关文章:

javascript - 排除span标签内容并仅获取li和a标签内容

javascript - 如何使用日期时间选择器添加警报

java - 在没有js的情况下检测是否安装了flash

node.js - 使用 Chai Expect throw 不捕获 Promise 拒绝

javascript - Mac OS X 上的本地主机 :8080 is not loaded for Node. js

java - 使用 Java 使用 Selenium WebDriver 捕获浏览器日志

python - 如何使用 cssSelector :nth-child(n) to locate element in Python Selenium

Java爬虫获取所有第一方和第三方cookie

javascript - 如何将分钟格式化为小时和分钟,但仅在 > 60 分钟时显示小时?

javascript - 如何为 html5 音频播放器创建自定义时间线?