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 couldn't find anything useful.
My code is like this:
function render() {
// each 2 seconds call the createNewObject() function
if(eachTwoSecond) {
createNewObject();
}
requestAnimationFrame(render);
renderer.render(scene, camera);
}
Any Idea?
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:requestAnimationFrame
passes single parameter to your callback which indicates the current time (in ms) whenrequestAnimationFrame
fires the callback. You can use it to calculate time interval betweenrender()
calls.I had a similar problem and came up with this solution:
P.s.
120
is not seconds but frames.