js监控fps(随手笔记)

483 阅读1分钟
   useEffect(() => {
    let lastTime = performance.now();
    let frame = 0;
    // let afps = 0;
    let lastFameTime = performance.now();
    const arrFps = [60, 60, 60];
    const loop = () => {
      const now = performance.now();
      const fs = (now - lastFameTime);
      lastFameTime = now;
      let fps = Math.round(1000 / fs);
      frame += 1;
      if (now > 1000 + lastTime) {
        // setFpss(fps);
        fps = Math.round((frame * 1000) / (now - lastTime));
        frame = 0;
        arrFps.unshift(fps);
        arrFps.length = 3;
        const fplbol = isLowFPS(arrFps, 15, 2);
        if (fplbol === true) {
          // setShow(fplbol);
          report(label);
          // afps = 1;
        }
        // if (afps === 1 && !fplbol) {
        //   setTimeout(() => {
        //     setShow(fplbol);
        //     afps = 0;
        //   }, 2000);
        // }
        lastTime = now;
      }
      // console.log(fps);
      window.requestAnimationFrame(loop);
    };
    loop();
  }, []);

 在app端web view的h5页面做的一个监控fps值,每秒执行一次, 连续两次执行fps值小于15时会执行