Is there any case where setTimeout(.., 0) is better than requestAnimationFrame()?

Viewed 4639

I've been using setTimeout(function(){ ... }, 0) as a way to delay a function call by one tick.

Normally I've been using this method for when I am trying to directly manipulate the event loop to make sure things execute in certain order, and in most cases it has to do with UI.

And sometimes I could sense that "tick", especially when I'm using this to run some 3rd party JS library on an element.

But I recently discovered requestAnimationFrame. This pretty much achieve the same thing, but in more graceful way.

Now I'm curious, are there any cases where it's more beneficial to use the setTimeout(function(){ ... }, 0) over requestAnimationFrame(function(){ ... })?

3 Answers

Depends, some cases like animations requestAnimationFrame is better and some cases like realtime process I prefer setTimeout 0.

Why? Because requestAnimationFrame is like a setTimeout(func, 1000/60), so setTimeout 0 is faster.

But if you compare requestAnimationFrame vs setTimeout(func, 1000/60). The requestAnimationFrame will be more exact than setTimeout.

Related