How to determine the best “framerate” (setInterval delay) to use in a JavaScript animation loop?

后端 未结 5 924
逝去的感伤
逝去的感伤 2020-11-28 10:24

When writing a JavaScript animation, you of course make a loop using setInterval (or repeated setTimeout). But what is the best delay to use in the setInterval/setTimeout ca

5条回答
  •  余生分开走
    2020-11-28 10:59

    I would venture to say that a substantial fraction of web users are using monitors that refresh at 60Hz, which translates to one frame every 16.66ms. So to make the monitor the bottleneck, you need to produce frames faster than 16.66ms.

    There are two reasons you would pick a value like 13ms. First, the browser needs a little bit of time to repaint the screen (in my experience, never less than 1ms). Which puts you at, say, updating every 15ms, which happens to be a very interesting number - the standard timer resolution on Windows is 15ms (see John Resig's blog post). I suspect that an well-written 15ms animation looks very close to the same on a wide variety of browsers/operating systems.

    FWIW, fbogner is plain wrong about non-Chrome browsers firing setInterval every 20-30ms. I wrote a test to measure the speed of setInterval firing, and got these numbers:

    • Chrome - 4ms
    • Firefox 3.5 - 15ms
    • IE6 - 15ms
    • IE8 - 15ms

提交回复
热议问题