requestAnimationFrame 实现是递归的吗? [英] Is requestAnimationFrame implementation's recursive?

查看:35
本文介绍了requestAnimationFrame 实现是递归的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Wouldn't the following code result in infinite recursion before the cube rotations and renderer.render function are invoked?

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.

在我看来,render 是作为回调函数调用的.但这是否意味着 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.

查看这里.

这篇关于requestAnimationFrame 实现是递归的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆