简体   繁体   English

为什么我需要cancelAnimationFrame()

[英]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.

相关问题 如果我不需要结果列表中的CSS选择器的粒度,为什么我会想要在“实时”NodeList上使用静态NodeList / HTMLCollection? - Why would I ever want a static NodeList/HTMLCollection over a “live” NodeList if I don't need the granularity of CSS Selectors in the resulting list? 为什么cancelAnimationFrame 不起作用? - Why is cancelAnimationFrame not working? 为什么我会在HTML和CSS中的属性上使用类? - Why would I ever use a class over an attribute in HTML and CSS? 为什么我在使用QUnit编写测试时会使用expect()? - Why Would I ever use expect() When Writing Tests With QUnit? 为什么我要使用Array.toSource? - Why would I ever use Array.toSource? 为什么开发人员需要PhantomJS而不是仅使用某些测试框架? - Why would a developer ever need PhantomJS as opposed to just using some testing framework? 什么是摇树,我为什么需要它? - What is Tree Shaking and Why Would I Need It? 为什么我需要在 JavaScript 中冻结一个 object? - Why would I need to freeze an object in JavaScript? 为什么要使用$ rootScope? - Why would you ever use the $rootScope? 为什么 js window.cancelAnimationFrame() 不起作用? - Why the js window.cancelAnimationFrame() not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM