Facebook Pixel

Alternatives to setTimeout in Modern JavaScript

setTimeout is not always the right tool. Here are modern alternatives for different scheduling needs.

Alternatives to setTimeout in Modern JavaScript

setTimeout is not always the right scheduling tool. Modern JavaScript offers better alternatives for specific use cases.

1. requestAnimationFrame (For Animations)

function animate() { element.style.left = parseInt(element.style.left) + 1 + "px"; requestAnimationFrame(animate); } requestAnimationFrame(animate);

Syncs with the browser's refresh rate. Pauses when the tab is hidden. Use for: visual animations.

2. queueMicrotask (For Urgent Work)

queueMicrotask(() => { ... });

Runs before rendering and macrotasks. Use for: urgent work that must run before the next event.

3. requestIdleCallback (For Low-Priority Work)

requestIdleCallback((deadline) => { while (deadline.timeRemaining() > 0) doWork(); });

Runs when the browser is idle. Use for: analytics, pre-fetching.

4. scheduler.postTask (For Prioritized Scheduling)

scheduler.postTask(() => { ... }, { priority: "user-blocking" });

Newer API for prioritized scheduling. Use for: fine-grained priority control.

5. async/await (For Async Flow)

async function main() { const data = await fetch("/api"); process(data); }

Use for: readable async code.

6. Web Workers (For Heavy Computation)

const worker = new Worker("heavy.js");

Runs on a separate thread. Use for: CPU-intensive work.

The Takeaway

Use requestAnimationFrame for animations, queueMicrotask for urgent work, requestIdleCallback for low-priority work, scheduler.postTask for prioritized scheduling, async/await for async flow, and Web Workers for heavy computation.

requestAnimationFrame (animations), queueMicrotask (urgent work), requestIdleCallback (low-priority work), scheduler.postTask (prioritized scheduling), async/await (async flow), and Web Workers (heavy computation).

For animations and visual DOM updates. requestAnimationFrame syncs with the browser's refresh rate, runs before the next paint, and pauses when the tab is hidden.

A newer API for prioritized task scheduling. You can specify priorities: user-blocking, user-visible, or background. Use it when you need fine-grained control over task priority.

A callback that runs when the browser is idle (between frames). The deadline object tells you how much time is left. Use it for low-priority work like analytics and pre-fetching.

For CPU-intensive work that would block the main thread. Web Workers run on a separate thread, so the main thread stays responsive. setTimeout cannot solve blocking; it only defers it.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.