[英]Why would I ever need to cancelAnimationFrame()
I don't understand why the API includes cancelAnimationFrame()
, because I can stop the animation by setting the continue
variable like this: 我不明白为什么API包含
cancelAnimationFrame()
,因为我可以通过设置这样的continue
变量来停止动画:
function draw(timestamp) {
if (continue == true) { requestAnimationFrame(draw); }
}
So the questions is under what circumstances should I use cancelAnimationFrame()? 那么问题是在什么情况下我应该使用cancelAnimationFrame()?
First, an important point of reference. 首先,一个重要的参考点。 A (dated) (Candidate Recommendation) Spec: http://www.w3.org/TR/animation-timing/#definitions
A(注明日期)(候选推荐)规范: http : //www.w3.org/TR/animation-timing/#definitions
Associated with every Document is an animation frame request callback list, which is a list of tuples.
与每个Document相关联的是动画帧请求回调列表,它是元组列表。 handle is an integer
^[long]
that uniquely identifies the entry in the list.handle是一个整数
^[long]
,用于唯一标识列表中的条目。 callback is a FrameRequestCallback object.callback是一个FrameRequestCallback对象。 Initially, the animation frame request callback list for a Document is empty.
最初,Document的动画帧请求回调列表为空。
Also, lets bear in mind that the target audience of these specs are user-agent developers. 另外,请记住,这些规范的目标受众是用户代理开发人员。 It's in the implemention of these specs that user-agent provides an API to us (app developers) to interface with/to.
正是在这些规范的实现中,用户代理为我们(应用程序开发人员)提供了一个API来与之交互。
Note the every Document sentiment above; 请注意上面的每个文档情绪; you can have multible documents in a
window
, or context . 您可以在
window
或上下文中包含多个文档。 You can have multiple contexts in a browsing context. 你可以在一个浏览器上下文的多重执行绪 。
So, how does that relate to every Document ? 那么,这与每篇文档有何关系? Well, the (Recommendation) spec references the Process Model , which essentially dumps all these lists into an empty list, and perform a callback invoking algorithm on the results of that 'preserved' list.. but, again, this is probably not a concern to us as app developers;
好吧,(推荐)规范引用了过程模型 ,它基本上将所有这些列表转储到一个空列表中,并对“保留”列表的结果执行回调调用算法 ..但是,这可能不是一个问题作为应用程序开发人员; *I THINK , we, as app developers, aren't going to be tracking and maintaining even Document.FrameRequestList instances in and throughout multiple documents of our own
window
context . *我认为 ,作为应用程序开发人员,我们不会在我们自己的
window
上下文的多个文档中及其中跟踪和维护Document.FrameRequestList实例。 We just interface with the API, which is accessible on window
. 我们只是与API接口,可在
window
访问。
Now, lets summarize what requestAnimationFrame(<function>)
does in effect, AND what it returns . 现在,让我们总结一下
requestAnimationFrame(<function>)
确实有效, 并 返回什么。
Calling requestAnimationFrame
and providing a function
as a callback , adds an entry ( <handler,FrameRequestCallback>
or <long, "an object, with a cancelled member, that encapsulates your function">
) into an animation frame request callback list . 调用
requestAnimationFrame
并提供function
作为回调 ,将一个条目 ( <handler,FrameRequestCallback>
或<long, "an object, with a cancelled member, that encapsulates your function">
)添加到动画帧请求回调列表中 。 requestAnimationFrame
returns the Handler [long]
. requestAnimationFrame
返回Handler [long]
。
According to the aforementioned spec ( http://www.w3.org/TR/animation-timing/#dom-windowanimationtiming-cancelanimationframe ), 根据上述规范( http://www.w3.org/TR/animation-timing/#dom-windowanimationtiming-cancelanimationframe ),
The cancelAnimationFrame method is used to cancel a previously made request to schedule an animation frame update.
cancelAnimationFrame方法用于取消先前生成的调度动画帧更新的请求。
One can infer, that by calling cancelAnimationFrame
and supplying the (presumably previously stored) handle
as a parameter, you remove an entry from the animation frame request callback list , BUT that isn't the case. 可以推断,通过调用
cancelAnimationFrame
并提供(可能是先前存储的) handle
作为参数,您可以从动画帧请求回调列表中 删除一个条目, 但事实并非如此。
When cancelAnimationFrame(handle) is called, the user agent must set the cancelled flag to true for the callback registered on this Document whose handle is handle .
当调用cancelAnimationFrame(handle)时,用户代理必须将已取消的标志设置为true,以便在其句柄为句柄的此Document上注册的回调。 The cancelled flag is set whether the callback is in a animation frame request callback list or not.
设置取消标志是否回调是否在动画帧请求回调列表中 。 If there is no callback with the given handle, then this function does nothing.
如果给定句柄没有回调,则此函数不执行任何操作。
So the handle
you provide in your cancelAnimationFrame
doesn't modify the list. 因此,您在
cancelAnimationFrame
中提供的handle
不会修改列表。 it sets the cancelled flag to true 'on the callback'.. which really keeps it from running. 它将取消的标志设置为true'在回调'..这确实使它无法运行。 This is reasonable because the aforementioned (above) Processing Model .
这是合理的,因为前面提到的(上面) 处理模型 。
So, to your question (and in regards to a particular comment on your question) skipping over the adding of an entry to an animation frame request callback list of a document, using requestAnimationFrame
doesn't keep existing scheduled
entries (or existing entries whose cancelled flag is false) from running. 因此,对于您的问题(以及关于您的问题的特定评论)跳过添加文档的动画帧请求回调列表的条目,使用
requestAnimationFrame
不会保留现有的scheduled
条目(或已取消的现有条目)来自跑步的旗帜是假的。 There is a 'larger' context and processing model to consider (which includes document-page visibility attributes). 需要考虑“更大”的上下文和处理模型(包括文档页面可见性属性)。
It was mentioned as a comment in your question that there are particular scenerios that you might want to cancel the schedule frame requests - but better reasoning to do it is for the unintentional aspects and considerations. 在您的问题中,有人提到您可能希望取消计划框架请求的特定场景 - 但更好的理由是针对无意的方面和注意事项。
In short, if you're going to use the API to request frame updates that do callbacks, use the API to cancel/stop said update requests and stop the callback. 简而言之,如果您要使用API来请求执行回调的帧更新,请使用API取消/停止所述更新请求并停止回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.