August Feng

Calculating monitor refresh rate using javascript

About

In order to learn a bit more about requestAnimationFrame, I write some code to calculate and log a monitor's refresh rate.

  function callback() {
      let n = 0;
      let start = performance.now();

      requestAnimationFrame(function _() {
          if (performance.now() - start < 1000) { n = n + 1; requestAnimationFrame(_); }
          else { console.log(n / 1); }
      });
  }

  requestAnimationFrame(callback);