Say I run a 2-step process during page scrolling, that I'd like to split up for performance reasons (preventing jank). I could use setTimeout
:
function foo() {
// Step 1
setTimeout(bar, 25);
}
function bar() {
// Step 2
}
However, I could also use requestAnimationFrame (rAF):
function foo() {
// Step 1
requestAnimationFrame(bar);
}
function bar() {
// Step 2
}
Are there benefits of using rAF instead of a plain timeout? Note that my "Step 2" does not write to the DOM. I just want to yield in order to prevent jank.