是的setInterval CPU密集型? [英] Is setInterval CPU intensive?

查看:195
本文介绍了是的setInterval CPU密集型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说的setInterval是CPU密集型的。我创建了一个使用setInterval的脚本和监控CPU的使用率却没有注意到的变化。我想知道如果有什么我错过了。

I read somewhere that setInterval is CPU intensive. I created a script that uses setInterval and monitored the CPU usage but didn't notice a change. I want to know if there is something I missed.

什么是code的作用是检查在URL中对哈希(#后的内容),每100毫秒,如果它已经改变了,使用AJAX加载的页面。如果没有改变,没有任何反应。会不会有任何的CPU的问题这一点。

What the code does is check for changes to the hash in the URL (content after #) every 100 milliseconds and if it has changed, load a page using AJAX. If it has not changed, nothing happens. Would there be any CPU issues with that.

推荐答案

我不认为的setInterval 本质上是要引起你显著的性能问题。我怀疑的声誉可能会从较早的时代,当CPU的不太厉害。

I don't think setInterval is inherently going to cause you significant performance problems. I suspect the reputation may come from an earlier era, when CPUs were less powerful.

有方法可以但提高性能,以及它可能是明智的,做他们:

There are ways that you can improve the performance, however, and it's probably wise to do them:

  1. 在传递一个函数的setInterval ,而不是一个字符串。
  2. 有少的时间间隔设置为可能的。
  3. 请区间持续时间尽可能长的时间。
  4. 有code运行的每个时间短,越简单越好。
  1. Pass a function to setInterval, rather than a string.
  2. Have as few intervals set as possible.
  3. Make the interval durations as long as possible.
  4. Have the code running each time as short and simple as possible.

不优化prematurely - 不要难为自己,当没有问题

Don't optimise prematurely -- don't make life difficult for yourself when there isn't a problem.

但有一件事,你可以在特定情况下做的是使用 onhashchange 事件,而不是超时,以支持它的浏览器。

One thing, however, that you can do in your particular case is to use the onhashchange event, rather than timeouts, in browsers that support it.

这篇关于是的setInterval CPU密集型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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