JavaScript Wait 1 Second: Practical Delay Patterns

Master practical JavaScript timing patterns to wait 1 second without blocking. Explore setTimeout, Promises, async/await, cancellation, and real-world usage with clear, working examples.

JavaScripting
JavaScripting Team
·5 min read
One-Second Wait - JavaScripting
Photo by ClickerHappyvia Pixabay
Quick AnswerSteps

To wait exactly one second in JavaScript, prefer asynchronous delays over blocking. Common approaches: 1) Callback style with setTimeout: setTimeout(() => { /* after 1000ms */ }, 1000); 2) Promise-based delay with async/await: await new Promise(resolve => setTimeout(resolve, 1000)); This pauses execution inside an async function for roughly one second before continuing.

Understanding asynchronous delays vs blocking

In JavaScript, there is no built-in sleep that blocks the thread like in some languages. Instead, you schedule work for the future using asynchronous delays. The keyword here is 'javascript wait 1 second' as a pattern: you pause action without freezing the UI by letting the event loop run other tasks in the meantime.

JavaScript
// Dangerous example: busy-waiting (not recommended) // This loops for roughly 1 second and blocks the event loop const start = Date.now(); while (Date.now() - start < 1000) { /* busy-wait */ } console.log('done');

The busy-wait demonstrates why you should avoid synchronous sleeps. Instead, use timer-based or promise-based delays to yield control back to the runtime.

According to JavaScripting, mastering asynchronous delays is essential to keeping UIs responsive during waits.

descriptionSectionOnlyForMarkdownWritesAllowedAndNoExtraTextEnd

Steps

Estimated time: 20-30 minutes

  1. 1

    Define a delay utility

    Create a reusable delay(ms) function that returns a Promise, causing the caller to await the duration.

    Tip: Name it delay and export for reuse
  2. 2

    Use delay in an async flow

    In an async function, await delay(1000) to pause for ~1s before continuing.

    Tip: Chain with other awaits for readable sequencing
  3. 3

    Add cancellation

    Attach an AbortController to cancel the delay when needed.

    Tip: Abort early to keep UI responsive
  4. 4

    Measure actual wait

    Wrap delay with performance.now() to compute drift.

    Tip: Expect small variations due to tab throttling
  5. 5

    Integrate into a real task

    In a polling loop, delay between checks to avoid busy-waiting.

    Tip: Test under load to ensure tolerance
Pro Tip: Prefer Promise-based delays for readability and testability.
Warning: Avoid busy-wait loops; they block the event loop and freeze the UI.
Note: In browsers, timers can be throttled in background tabs; design for jitter.

Prerequisites

Required

Optional

  • Code editor or IDE
    Optional

Commands

ActionCommand
Run a one-second delay in Node.jsOne-liner to test a 1s delay in Node.jsnode -e "async function w(ms){ await new Promise(r => setTimeout(r, ms)); console.log('done'); } w(1000)"
Test delay in browser console with async/awaitPaste into browser consoleasync function w(ms){ await new Promise(r => setTimeout(r, ms)); console.log('done'); } w(1000)

Questions & Answers

What is the difference between setTimeout and setInterval for delays?

setTimeout schedules a single callback after the delay; setInterval repeats at fixed intervals until cleared. For a one-second wait, setTimeout is typically used to delay a single action; setInterval is better for recurring tasks but can drift if the callback takes longer than the interval.

setTimeout runs once after the delay, while setInterval repeats until you stop it.

Is a 1000ms delay exact?

No. The actual delay depends on the event loop, timer resolution, and rendering work. In inactive tabs or under heavy load, it can be longer than 1000ms.

No—timers can drift due to how the event loop and rendering work.

Can delays block the UI?

If implemented with a busy-wait or synchronous sleep, the UI is blocked. Proper delays use asynchronous timers that yield control back to the event loop.

Delays implemented asynchronously don’t block the UI.

How can I cancel a delay?

Use AbortController (where supported) or a cancellation flag to stop a pending delay before it completes.

You can cancel a delay with an abort signal or a flag.

Do delays work in Node and the browser?

Yes, both environments support delay patterns, but browser timers can be throttled in background tabs and Node timers depend on the event loop under load.

Delays work in both, with environment-specific caveats.

How do I handle delays in animations or polling?

Use short, reusable delay utilities and combine with loops or animation frames for smooth UX. Avoid blocking calls in animation logic.

Combine delays with non-blocking animation patterns for smooth results.

What to Remember

  • Use Promise-based delays with async/await for clean sequencing
  • Delay helpers improve reusability and testability
  • Delays are non-blocking and rely on the event loop
  • Cancellation improves responsiveness during navigation
  • Measure actual wait to assess drift and reliability

Related Articles