使用console.time

发布于 2024-12-11 11:55:52 字数 1173 浏览 0 评论 0原文

我正在使用 Google Closure Library 及其编译器创建一个应用程序。为了调试值,我使用console.log()。编译此文件将引发以下异常JSC_UNDEFINED_VARIABLE。变量 console 未在 ... 处声明。要解决此错误,我只需使用 window.console.log() 即可。
我还想测量一个函数所花费的时间。 Firebug 有两个很好的函数 console.time(name)console.timeEnd(name) 可以很轻松地做到这一点。不幸的是,闭包编译器不支持这些函数,并抛出以下警告 JSC_INEXISTENT_PROPERTY。从未在 Window.prototype.console 上定义属性时间 ...。不幸的是,您无法通过添加 window 来解决此警告。
我也查看了该库,但 goog.debug.Console 没有我需要的功能。
我之前使用过的另一个解决方案如下所示。

var start = new Date();  
// do something
var end = new Date();

// do some calculation to get the ms for both start and end
var startMS = ....;
var endMS = .....;
// get the difference and print it
var difference = (endMS - startMS) / 1000;
console.log('Time taken for something: ' + difference);

如果您经常使用它,代码有点太多,并且具有两个函数的版本会很棒:

window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");

这会打印出开始和结束之间的 MS。但如上所述,这不适用于闭包编译器。有谁有解决方案,我如何使用这两个函数window.console.time()window.console.timeEnd()?或者可能是 goog.closure 提供的另一个解决方案,但我还没有找到?

I'm creating an application with Google Closure Library and its Compiler. To debug values I use console.log(). Compiling this will throw the following exception JSC_UNDEFINED_VARIABLE. variable console is undeclared at .... To solve this error, I just had to use window.console.log() instead.
I also want to measure the time that a function takes. Firebug has two nice functions console.time(name) and console.timeEnd(name) to do that very easily. Unfortunately the Closure Compiler does not support these functions throwing the following warning JSC_INEXISTENT_PROPERTY. Property time never defined on Window.prototype.console at .... Unfortunately you cannot solve this warning with prepending window.
I also had a look at the library, but goog.debug.Console has not the function that I need.
Another solution I have used before was something like the following

var start = new Date();  
// do something
var end = new Date();

// do some calculation to get the ms for both start and end
var startMS = ....;
var endMS = .....;
// get the difference and print it
var difference = (endMS - startMS) / 1000;
console.log('Time taken for something: ' + difference);

This is a little bit too much code, if you use it very often, and the version with the two functions would be great:

window.console.time("timeTaken");
// do something
window.console.timeEnd("timeTaken");

This prints out the MS between start and end. But as mentioned above, this doesn't work with the closure compiler. Does anyone have a solution for this, how I can use these two functions window.console.time() and window.console.timeEnd()? Or maybe another solution that goog.closure provides, but I haven't found?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2024-12-18 11:55:52

您只需将它们添加到您正在使用的 extern 中即可。

You just need to added them to the externs you are using.

骄傲 2024-12-18 11:55:52

如果您不想/不能使用 extern,您可以轻松地使用基于字符串的属性引用“未声明”对象:

window['console']['log']('Hello Console!');
window['console']['time']('timeTaken');
...

但是您必须小心,因为如果 time 属性不存在或者不是函数。

If you don't want to/can't use externs, you can easily reference "undeclared" objects with the string-based properties:

window['console']['log']('Hello Console!');
window['console']['time']('timeTaken');
...

But you have to be careful, because the second line might throw an error if the time property does not exist or it's not a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文