Is it possible to find out what is the monitor frame rate in javascript?

  1. First, Is it possible to find out
    what is the monitor frame/refresh rate in
    javascript (60Hz for most LCD monitors)?
  2. Second, is there any way
    to say execute a function after
    every X frames?

Several people asked why I need this. Here is the context: I have an animation (an endless loop that renders one frame after another). The output of each iteration needs to be synchronized with monitor refresh rate, otherwise tearing will happen. The way I am doing it right now is to use setTimeout(loop, 16) within the loop method. Its sort of working. The second parameter needs to be 1/(refresh rate), and that is why I asked this question.

Answer

You may have some luck on modern browsers using window.requestAnimationFrame with a trivial callback that measures the time between successive invocations and from that calculate the FPS.

You should also be able to easily skip your render function every nth invocation to reduce the desired frame rate.

I put a rough example at http://jsfiddle.net/rBGPk/ – the math may be slightly wrong but it should be enough to show the general idea.