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

setTimeout vs. requestAnimationFrame? #2

Open
matthewmueller opened this issue Mar 4, 2016 · 2 comments
Open

setTimeout vs. requestAnimationFrame? #2

matthewmueller opened this issue Mar 4, 2016 · 2 comments

Comments

@matthewmueller
Copy link
Contributor

hey just wondering if there was any reason you went with a setTimeout in the renderer here? just out of simplicity?

I'm thinking requestAnimationFrame might be a bit better (to render on the paints) but i'm not sure if setTimeout was a deliberate decision or not.

@developit
Copy link
Owner

It was definitely just for simplicity. Technically there isn't a real reason to debounce rendering at all in preact-cycle, so it could just be made synchronous. I think my original intent with the setTimeout was to support animation (since the frame delay is done for you, you just have to call the next mutation in order to ask for another frame. If that's the only real use-case though, you're right in thinking requestAnimationFrame would be a better fit.

Some context: Preact itself originally used setTimeout(0), but switched to requestAnimationFrame() in 3.x. In 4.x it tries to use the DOM for tight async but falls back to setTimeout:
https://github.com/developit/preact/blob/master/src/util.js#L125-L137
There is also an option to turn off async altogether (options.syncComponentUpdates=true), which might be worth honoring here too.

@stereobooster
Copy link

Just FYI requestAnimationFrame is not triggered if tab is not visible.
setTimeout(0) has about 4ms delay in modern browsers (it was about 15ms in old). Also setTimeout "slows down" in invisible tab.

Alternatives:

To read more: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

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

No branches or pull requests

3 participants