Javascript - Can't Adjust FrameRate - requestanimationframe

前端 未结 7 2064
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 03:50

I start the loop

function gameLoop(){
   update();
   draw();
   requestAnimFrame(gameLoop);
}

var requestAnimFrame =  window.requestAnimationFrame ||
                  


        
7条回答
  •  庸人自扰
    2021-02-06 04:27

    A bit late to the party, but here's how to get the benefit of RAF while also controlling frames/second.

    Note: requestAnimationFrame now has a better way of doing things than by using the code pattern in my original 3 year old original answer ... see my update below for the new and improved way.

    [Update: requestAnimationFrame now has a better way of throttling]

    The new version of requestAnimationFrame now automatically sends in a current timestamp that you can use to throttle your code execution.

    Here is example code to execute your code every 1000ms:

    var nextTime=0;
    var delay=1000;
    
    function gameLoop(currentTime){
        if(currentTime

    }

提交回复
热议问题