【问题标题】:get console.timeEnd() to not log all by itself让 console.timeEnd() 不自己记录所有
【发布时间】:2022-11-21 11:54:46
【问题描述】:

我想知道该过程完全完成所花费的时间。我有这段代码:

console.log(`Ran ${ran} equations in ${console.timeEnd()}`)

但我没有得到预期的输出,即:

Ran X equations in 33.099ms // eg

相反我得到

default: 33.099ms
Ran X Equations in undefined

请注意,我没有给我的console.time() 贴上标签。
我怎样才能达到我的预期输出?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    console.timeEnd() 返回 undefined,而不是它记录的内容。要非常接近所需的输出,请使用描述性字符串标记计时器...

    const ran = 4; // you'll need to know ran before starting the timer
    const label = `Ran ${ran} equations in`;
    console.time(label);
    // do some time consuming stuff
    alert('wait a sec, then press ok')
    console.timeEnd(label);

    【讨论】:

      猜你喜欢
      • 2018-04-29
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多