CPU 对 JavaScript setInterval 的影响有多大

发布于 2024-12-07 17:33:45 字数 449 浏览 5 评论 0原文

我写了一个小的 jquery 插件,它基本上将 html 元素中的所有单词转换为 span,使它们不可见,然后将它们动画化到视图中。我这样做是为了让您可以定义加载整个元素所需的时间,并且根据测试,数学似乎是正确的,但实际上需要更长的时间。

请参阅jsfiddle: http://jsfiddle.net/A2DNN/

注意变量“per”和“ms”,这基本上告诉它每“ms”毫秒处理“每个”单词数。

在日志中,您将看到它将每 1 毫秒处理 1 个单词,这应该会导致加载时间更快。

所以我只是想知道,CPU 是否有可能在这里形成瓶颈? JS 使项目淡入视图,这是由 CPU 处理的,CPU 的图形处理速度不是很快。

这个问题听起来几乎很愚蠢,我希望现在的 CPU 会嘲笑这样的一小部分工作负载。

I wrote a small jquery plugin that basically converts all words in an html element to spans, makes them invisible and then animates them into view. I made it so that you can define the time that it's supposed to take to load the whole element, and based on tests the math seems to be right, but in practice it takes quite a bit longer.

See jsfiddle: http://jsfiddle.net/A2DNN/

Note the variables "per" and "ms", this basically tells it to process "per" number of words every "ms" milliseconds.

In the log you'll see it'll process 1 word ever 1 ms, which should result in a MUCH faster loading time.

So I'm just wondering, is it possible that the CPU is forming a bottleneck here? In that JS is fading items into view, which is handled by the CPU, which isn't very fast at graphical processing.

It sounds almost silly to ask, I'd expect these days a CPU would laugh at a small bit of work load like this.

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

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

发布评论

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

评论(1

ま昔日黯然 2024-12-14 17:33:45

这是由于浏览器的 JavaScript 实现强制设置了最小超时。你不能有 1 毫秒的超时,它会比这个稍微长一点。已经有一个讨论 关于这个。

It is due to a minimum timeout forced by the browser's JavaScript implementation. You can't have a 1ms timeout, it is slightly more than that. There has already been a discussion about that here.

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