Re: The need to re-subscribe to requestAnimationFrame

On Mar 2, 2013 6:32 AM, "Florian Bösch" <pyalot@gmail.com> wrote:
>
> You can also wrap your own requestAnimationFrameInterval like so:
>
> var requestAnimationFrameInterval = function(callback){
>   var runner = function(){
>     callback();
>     requestAnimationFrame(runner);
>   };
>   runner();
> }
>
> This will still stop if there's an exception thrown by callback, but it
lets you write a cleaner invocation like so:
>
> requestAnimationFrameInterval(function(){
>   // do stuff
> });
>
> It does not give you a way to stop that interval (except throwing an
exception), but you can add your own if you're so inclined.
>
> Notably, you could not flexibly emulate requestAnimationFrame (single)
via requestAnimationFrameInterval, so if you're gonna pick one semantic to
implement, it's the former rather than the latter.

For what it's worth, this would have been another (maybe better) way to
address the concern that current spec tries to solve by requiring
reregistration.

I.e. we could have defined a

id = requestAnimationFrameInterval(callback)
cancelAnimationFrameInterval(id)

Set of functions which automatically cancel the interval if an exception is
thrown.

That reduces the current risk that people write code that reregister at the
top, and then has a bug further down which causes an exception to be thrown.

/ Jonas

>
>
> On Sat, Mar 2, 2013 at 3:15 PM, Glenn Maynard <glenn@zewt.org> wrote:
>>
>> On Sat, Mar 2, 2013 at 5:03 AM, David Bruant <bruant.d@gmail.com> wrote:
>>>
>>> If someone wants to reuse the same function for
requestionAnimationFrame, he/she has to go through:
>>>     requestAnimationFrame(function f(){
>>>         requestAnimationFrame(f);
>>>         // do stuff
>>>     })
>>
>>
>> FYI, this pattern is cleaner, so you only have to call
requestAnimationFrame in one place:
>>
>> function draw() {
>>     // render
>>     requestAnimationFrame(draw);
>> }
>> draw();
>>
>> --
>> Glenn Maynard
>>
>

Received on Friday, 8 March 2013 18:43:35 UTC