Call a function each x second in requestAnimationFrame

前端 未结 3 1914
深忆病人
深忆病人 2021-01-02 04:25

I\'m working on some personal project by Three.js. I\'m using requestAnimationFrame function. I want to call a function each 2 seconds. I\'ve search but I could

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 05:09

    I had a similar problem and came up with this solution:

    let i = 0
    function render() {
       if (++i % 120 == 0) doSomething()
       requestAnimationFrame(render)
    }
    

    P.s. 120 is not seconds but frames.

提交回复
热议问题