0

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?

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. But my question is more in regards to performance. I see two conflicting realities as X grows smaller:

  • As X grows smaller, perceived "responsiveness" grows - a positive result.
  • As X grows smaller, more system processing power is consumed - a negative result.

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.

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? 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?

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? Is there a definitive method?

fildred13
  • 2,280
  • 8
  • 28
  • 52
  • setInterval(fn,0) fires about 250 times a second, but setTimeout doesn't like anything under 12. i wouldn't trust anything under 32ms not to get backed-up eventually. – dandavis May 31 '14 at 17:27
  • that said, use prop=myVar;Object.defineProperty( window, "myVar", set: function(v){alert(v); prop=v;}); instead of polling window to react to property changes. – dandavis May 31 '14 at 17:29
  • See http://stackoverflow.com/questions/9647215/what-is-minimum-millisecond-value-of-settimeout for discussion of browser support for short times. – jfriend00 May 31 '14 at 17:34
  • Voting to close this question because there is no way to answer this without a very, very detailed specific situation and specific device. As such, there really is no answer to this question. Because it's so hard to answer in the real world, a different/better design should be chosen that does not rely on or need a super short interval. – jfriend00 May 31 '14 at 17:36
  • @dandavis: Your solution is a little hard for me to undestand. Could you expand or link to docs that describe? My question is moot if the entire method of testing for window changes is etter served by another method. – fildred13 May 31 '14 at 17:36
  • @jfriend00: I'm struggling to ask this question in a way that isn't open ended. I tried to add focus with an edit. How should I be asking this question? – fildred13 May 31 '14 at 17:39
  • @jfriend00 The linked question is very helpful. That more or less answers the question. I guess this should be marked as a duplicate? OR should I just delete it? – fildred13 May 31 '14 at 17:42
  • I didn't mark it a dup of that link because you didn't ask what's the shortest time that the browser supports (which is what that link answers)? You asked a much more nebulous question about the shortest time that maintains performance which is pretty much unanswerable. I think you need a better design for whatever problem you're trying to solve. Fast polling is never the best choice. – jfriend00 May 31 '14 at 17:52

1 Answers1

-1

Run the function and time the execution, when you have the execution time in your hand put that value in the setInterval + some spare time..

Erik Simonic
  • 457
  • 3
  • 13