浏览器控制台并计算多个javascript执行时间差 [英] Browser console and calculating multiple javascript execution time differences

查看:162
本文介绍了浏览器控制台并计算多个javascript执行时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以轻松做到这一点:

I can easily do this:

console.time('mytimer');
doSomeWork();
console.timeEnd('mytimer');

但是可以用多个函数计算时间.我需要在全局变量中定义脚本的开始时间.然后,在多个函数中,我将写入自时间开始以来经过的毫秒数.并编写函数名称,如下所示:

But is it possible to calculate time in multiple functions. I need to define the script's start time in a global variable. Then inside multiple functions I will write how many miliseconds passed since the start of the time. And write the name of the function Something like this:

console.time('mytimer');
doSomeWork() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork())
};
doSomeWork2() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork2())
};
doSomeWork3() {
  // console.log(difference between now and "mytimer"s start time)
  // console.log(name of the function: doSomeWork3())
};
console.timeEnd('mytimer');

我将在Chrome 26+中使用它来解决调试问题,因此使用依赖于浏览器的功能(例如:arguments.callee.name)是没有问题的.

I will use this in Chrome 26+ for debug issues so using browser dependent functions (for example: arguments.callee.name) is not a problem.

以解决我的问题.
这有效:

To clearize my problem.
This works:

console.time('myTimer1');
console.timeEnd('myTimer1');

这不起作用:

console.time('myTimer2');
console.time('myTimer2');

当然可以写太多的计时器并检查每个计时器的时间.但是我需要知道自从在每圈开始启动javascript代码以来所经历的时间.

Of course it is possible to write too much timers and check time of each of them. But I need to know elapsed time since the javascript code is started in each lap.

推荐答案

有一种解决方案是生活水平.好像已经在chrome以及主要的浏览器和nodejs上使用了

There is a solution which is a living standard. Seems like its already on chrome and major browsers and nodejs

https://developer.mozilla.org/en -US/docs/Web/API/Console/timeLog

console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");
The

这篇关于浏览器控制台并计算多个javascript执行时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆