eval() 的线程行为在不同浏览器中是否有所不同?

发布于 2024-07-12 20:00:52 字数 719 浏览 7 评论 0原文

我目前正在记录一个 AJAX 应用程序,其中包含包含某些交互时间的消息。 因此,我有几个地方的代码遵循这样的模式:

var startTime = new Date();
this.doFunction();
var endTime = new Date();
logger.log("doFunction took " + (endTime - startTime) + " milliseconds.");

我要做的是将计时分离到一个库函数中,该函数将函数作为参数,看起来像这样:(

time : function(toTime) {
    var startTime = new Date();
    eval(toTime);
    var endTime = new Date();
    logger.log(toTime + " took " + (endTime - startTime) + " milliseconds.");
} 

语法可能是错了,我对 JavaScript 不太熟悉)

这样我就不用计时了:

time(this.doFunction);

我的问题是,不同的浏览器在 eval() 方面有不同的行为吗? 例如将 eval 触发到一个新线程中,从而使我的计时不正确?

任何其他有关时间安排的建议将不胜感激。

I am currently logging an AJAX application with messages which include the times of certain interactions. So I have a couple of places where the code follows a pattern such as this:

var startTime = new Date();
this.doFunction();
var endTime = new Date();
logger.log("doFunction took " + (endTime - startTime) + " milliseconds.");

What I'm look to do is separate the timing into one library function, which takes a function as a parameter, to look something like:

time : function(toTime) {
    var startTime = new Date();
    eval(toTime);
    var endTime = new Date();
    logger.log(toTime + " took " + (endTime - startTime) + " milliseconds.");
} 

(Syntax may be wrong, I'm not too familiar with JavaScript)

So that then instead of doing the timing I would just do:

time(this.doFunction);

My question is, do different browsers have different behaviour when it comes to eval()? Such as firing off the eval into a new thread, thus rendering my timing incorrect?

Any other advice on timing would be appreciated.

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

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

发布评论

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

评论(2

楠木可依 2024-07-19 20:01:02

Eval 应该是同步的。

您不应该使用 eval() 而应该使用 toTime.call(),因为您应该避免使用 eval。

Eval should be synchronous.

You shouldn't use eval() but toTime.call(), because you should avoid using eval.

深巷少女 2024-07-19 20:00:59

不。所有浏览器的 JavaScript 引擎都是单线程的。 我怀疑您也可以通过简单地调用 toTime() 作为函数而不是使用 eval() 来解决这个问题。 您可能想查看 javascript arguments 对象和 javascript“call" 和 "apply" 方法透明地转发传递给外部的参数“time”函数到内部“toTime”函数。

No. All browsers are single-threaded in the javascript engine. I suspect that you can also solve this problem simply by invoking toTime() as a function instead of using eval(). You may want to look into the javascript arguments object and the javascript "call" and "apply" methods to transparently forward the arguments passed to your outer "time" function to the inner "toTime" function.

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