| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta
- name="viewport"
- content="width=device-width, initial-scale=1.0"
- />
- <title>Document</title>
- </head>
- <body>
- <script>
- /**
- * @param {number} targetCount 不小于1的整数,表示经过targetCount帧之后返回结果
- * @return {Promise<number>}
- */
- const getScreenFps = (() => {
- // 先做一下兼容性处理
- const nextFrame =
- window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame;
- if (!nextFrame) {
- console.error('requestAnimationFrame is not supported!');
- return;
- }
- return (targetCount = 120) => {
- // 判断参数是否合规
- if (targetCount < 1)
- throw new Error('targetCount cannot be less than 1.');
- const beginDate = Date.now();
- let count = 0;
- return new Promise((resolve) => {
- (function log() {
- nextFrame(() => {
- if (++count >= targetCount) {
- const diffDate = Date.now() - beginDate;
- const fps = (count / diffDate) * 1000;
- return resolve(fps);
- }
- log();
- });
- })();
- });
- };
- })();
- getScreenFps().then((res) => {
- console.log(res);
- });
- </script>
- </body>
- </html>
|