requestAnimationFrame实现递归吗?

问题描述:

我目前正在尝试Three.js,它依赖requestAnimationFrame来执行动画。

I'm currently experimenting with three.js, which relies on requestAnimationFrame to perform animations.

问题:以下代码是否会导致无限

function render() {
    requestAnimationFrame(render);
    cube.rotation.x += 0.1;
    cube.rotation.y += 0.1;
    renderer.render(scene, camera); 
}
render();

该代码有效,但我想提高对javascript的整体理解。

The code works, but I'm trying to improve my overall understanding of javascript.

我看到的方式是,渲染作为回调函数被调用。但这是否意味着javascript会继续运行 之前 函数中的代码,停止继续进行下一个调用?

The way I see it, is that render is invoked as a callback function. But does that mean that javascript continues running through the code in the function before stopping to move on to the next call?

这仅请求浏览器在下一个渲染循环之前调用您的回调:

This only requests the browser to call your callback before the next rendering loop:


准备在屏幕上更新动画时,应调用此方法。这将要求您的动画函数在浏览器执行下一次重新绘制之前被调用。

You should call this method whenever you're ready to update your animation onscreen. This will request that your animation function be called before the browser performs the next repaint.

因此这里没有递归,您的函数将继续执行。

So there is no recursion here, and your function continue with the execution.

您还可以使用 cancelAnimationFrame 取消回调请求。

You can also cancel the request for your callback with cancelAnimationFrame.

查看此处