Loop forever and provide delta time

前端 未结 4 756
别跟我提以往
别跟我提以往 2021-01-03 07:01

I\'m writing an HTML5 Game Development Javascript framework and I want to provide the user the difference in time between the last tick and the current one.

         


        
4条回答
  •  春和景丽
    2021-01-03 07:12

    This is what I use:

    var lastTick = performance.now()
    
    function tick(nowish) {
      var delta = nowish - lastTick
      lastTick = nowish
    
      update(delta)
      render(delta)
    
      window.requestAnimationFrame(lastTick)
    }
    
    window.requestAnimationFrame(lastTick)

提交回复
热议问题