Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestAnimationFrame instead of setTimeOut #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Zae
Copy link

@Zae Zae commented Nov 10, 2011

I've implemented requestAnimationFrame instead of setTimeOut, should be a bit more efficient.

It uses a fallback to setTimeOut if the browser does not support requestAnimationFrame.

I also used the Google Closure Compiler to create the minified version.*

@cowboy
Copy link
Owner

cowboy commented Nov 10, 2011

What's the benefit to using requestAnimationFrame for non-animation tasks?

@Zae
Copy link
Author

Zae commented Nov 10, 2011

Mostly the standard benefits of requestAnimationFrame.

Hopefully the resize will be detected in the same frame as the resize actually happens, causing less delay between the resize and the event being triggered and better synchronizing the event with CSS Transformations and animations.

Also requestAnimationFrame uses a smart way of measuring the time before starting the next. This way there is no set delay between polling but the browser calculates the best time when to update.
This way the browser will automatically throttle the updates when the hardware is underpowered or for example the tab is not active.

The drawbacks of this method are that the browser chooses how often the event is fired and not the standard delay you set. Also it might cause more redraws than needed. The redraw problem could be fixed by giving the requestAnimationFrame function the context where the resize will be happening. But the single event loop design would need to be changed for that.

Giving context to requestAnimationFrame could also lead to better optimization for example when the element is scrolled out of view.

@cowboy
Copy link
Owner

cowboy commented Nov 10, 2011

In your solution, when using requestAnimationFrame, how would you clear a timeout?

@Zae
Copy link
Author

Zae commented Nov 10, 2011

Hmm, did not think about that.

There is a clearTimeOut() version for requestAnimationFrame tho.

I'll look at it tonight and update the patch.

Added a shim for cancelAnimationFrame
Updated requestAnimationFrame to return timeout_id
Added routine to make sure the loop is stopped (Firefox does not yet implement cancelAnimationFrame)
@Zae
Copy link
Author

Zae commented Nov 12, 2011

Had to add some code to make sure the loop is cancelled.

Firefox doesn't implement cancelAnimationRequest yet. This also makes sure the loop is cancelled if the clear method is called while the code in the loop is running.

*Used closure compiler
@Spotnyk
Copy link

Spotnyk commented Aug 23, 2012

Has this been implemented? If not, has a reason been given?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants