requestAnimationFrame 实现是递归的吗?

the*_*nut 6 javascript animation requestanimationframe

我目前正在试验three.js,它依赖于requestAnimationFrame执行动画。

renderer.render调用立方体旋转和函数之前,以下代码不会导致无限递归吗?

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

代码有效,但我正在努力提高我对 JavaScript 的整体理解。

在我看来,render 是作为回调函数调用的。但这是否意味着 JavaScript停止继续下一次调用之前会继续运行函数中的代码?

ela*_*con 8

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

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

所以这里没有递归,你的函数继续执行。

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

这里