简体   繁体   English

JavaScript中是否有最小可接受的计时器间隔?

[英]Is there a minimum acceptable timer interval in javascript?

Given: 鉴于:

window.setInterval(function () {
    //test for a change in the window state and, if there has been one:
    //make some changes
}, X); //repeat every X milliseconds

Is there a minimum acceptable value for X? X是否有最小可接受值?

Some reading lead me to this interesting article, which suggests that, even if X = 1 , in reality the browser will run the interval only 10-15ms. 一些阅读使我想到了这篇有趣的文章,该文章表明,即使X = 1 ,实际上浏览器也只能运行10-15ms的时间间隔。 But my question is more in regards to performance. 但是我的问题更多是关于性能。 I see two conflicting realities as X grows smaller: 随着X的变小,我看到两个相互矛盾的现实:

  • As X grows smaller, perceived "responsiveness" grows - a positive result. 随着X变小,感知到的“响应度”也随之增加-这是一个积极的结果。
  • As X grows smaller, more system processing power is consumed - a negative result. 随着X变小,将消耗更多的系统处理能力-负面结果。

If system processing power was a non-issue, the obvious answer would be X = 1 because the moment the user changes the window, there is a more or less immediate response. 如果系统处理能力不是问题,那么明显的答案将是X = 1,因为在用户更改窗口的那一刻,或多或少会有即时响应。

I know that the knee-jerk reaction is "well it depends how depleted system resources are, and how complicated the function is." 我知道弯腰的反应是“嗯,这取决于系统资源的枯竭程度以及功能的复杂程度”。 But there must be some way to determine a reasonable lower bound. 但是必须有某种方法来确定合理的下限。 For example, is there a test suite I should be running on many system/browser combinations to know that X isn't so low that it is consuming to much processing power? 例如,是否有一个我应该在许多系统/浏览器组合上运行的测试套件,才能知道X并不是那么低,以至于消耗了很多处理能力? Or is the answer "no value for x can possibly consume enough system resources to be noticeable," based on some fact about javascript intervals that I don't know? 还是基于我不知道的有关JavaScript间隔的一些事实,答案是“ x的无价值可能消耗足够的系统资源以引起注意”?

Knowing the answer requires such specific details, a more refined question would be: 知道答案需要这些具体细节,那么一个更完善的问题将是:

How does one go about determining the minimum value for X given any project/function combination? 给定任何项目/功能组合,如何确定X的最小值? Is there a definitive method? 有确定的方法吗?

运行该函数并计时执行时间,当手中有执行时间时,将该值放入setInterval +一些空闲时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM