Concepts

Coalesce visual updates into one frame

Try the free JavaScript compiler

Application pattern that retains the latest value and schedules at most one pending frame callback. Reset the pending ID before further scheduling.

Formal Syntax & Structure

javascript syntaxjavascript
if (pending === null) pending = requestAnimationFrame(render)

Working Example

Example Snippetjavascript
(async () => {let pending=null,latest;const rendered=[];function update(x){latest=x;if(pending===null)pending=requestAnimationFrame(()=>{pending=null;rendered.push(latest);});}update(1);update(2);update(3);await new Promise(resolve=>requestAnimationFrame(resolve));console.log(JSON.stringify(rendered));})()
JavaScript Reference