Call a function each x second in requestAnimationFrame

前端 未结 3 1903
深忆病人
深忆病人 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 04:59

    Since requestAnimationFrame will give you an available frame in 60fps (if your browser can keep up with it) it seems perfectly fine to wait 2 seconds and request a frame. This way the browser will give you a frame exactly after these 2 seconds, which in most cases will be in an instant:

            function render() {
                // each 2 seconds call the createNewObject() function
                createNewObject();
                renderer.render(scene, camera);
            }
    
            setInterval(function () {
                requestAnimationFrame(render);
            }, 2000);
    

提交回复
热议问题