Need to make JavaScript wait 1 second (1000ms)? Use a non-blocking delay. JavaScript cannot truly “sleep” the main thread without freezing the page.
JavaScript wait 1 second (1000ms), the best options
Pick the pattern that matches your code style. All three schedule work later, they do not pause execution synchronously.
setTimeout(() => { ... }, 1000)for callback-based codeawait delay(1000)for async/await flows (recommended)await setTimeout(1000)in Node.js withnode:timers/promises
// 1) Callback style
setTimeout(() => {
console.log("after 1 second");
}, 1000);
Code language: JavaScript (javascript)
// 2) async/await style
await delay(1000);
console.log("after 1 second");
Code language: JavaScript (javascript)
// 3) Node.js (v15+) promise timer
import { setTimeout } from "node:timers/promises";
await setTimeout(1000);
Code language: JavaScript (javascript)
Option 1, Wait 1 second using setTimeout (browser or Node)
setTimeout is the classic “wait 1 second” tool. It schedules a callback to run later, it does not block your code.
console.log("Executed now");
// 1 second delay
setTimeout(() => {
console.log("Executed after 1 second");
}, 1000);
Code language: JavaScript (javascript)
The second argument is milliseconds. For 1 second, pass 1000.
setTimeout does not block, here’s the execution order
console.log("Executed now");
setTimeout(() => {
console.log("Executed after 1 second");
}, 1000);
console.log("Executed before the delay");
Code language: JavaScript (javascript)
Expected output order:
Executed nowExecuted before the delayExecuted after 1 second
Code after setTimeout runs immediately because the callback is queued for later.
Common setTimeout mistakes
- Forgetting that code after
setTimeoutruns right away - Nesting many timers and creating callback hell
- Using a string callback like
setTimeout("doStuff()", 1000), don’t do that
Option 2, JavaScript sleep 1 second with Promise + async/await (recommended)
If you’re searching for “javascript sleep 1 second”, this is the clean approach. You still are not blocking the thread, you’re just awaiting a Promise that resolves after 1000ms.
Copy-paste delay() helper
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await delay(1000);
Code language: JavaScript (javascript)
Drop that helper into your project, then use await delay(1000) anywhere inside an async function.
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function init() {
console.log("now");
await delay(1000);
console.log("after 1 second");
await delay(1000);
console.log("after 2 seconds");
}
init();
Code language: JavaScript (javascript)
CTA: Copy the delay() helper and use await delay(1000) in your async functions. It keeps timing code readable.
Example, sequential waits with async/await
Async/await shines when you need multiple waits in a row without nesting callbacks.
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function run() {
for (let i = 1; i <= 3; i++) {
console.log(`step ${i}`);
await delay(1000);
}
console.log("done");
}
run();
Code language: JavaScript (javascript)
Browser support
Promises and async/await work in modern browsers. If you must support very old browsers like IE11, you need a polyfill or transpile your code.
You can test it here:
Option 3, Node.js wait 1 second with timers/promises
In Node.js, you can use the built-in promise-based timers API. It reads nicely and avoids writing your own helper.
Node.js (v15+) built-in promise setTimeout
import { setTimeout } from "node:timers/promises";
console.log("now");
await setTimeout(1000);
console.log("after 1 second");
Code language: JavaScript (javascript)
Node.js CommonJS version
const { setTimeout } = require("node:timers/promises");
(async () => {
console.log("now");
await setTimeout(1000);
console.log("after 1 second");
})();
Code language: JavaScript (javascript)
When to use this vs delay()
Use node:timers/promises when you’re in Node 15+. Use a delay() helper when you want the same code to work in both browser and Node.
Avoid this, blocking “sleep” loops freeze the page
A busy-wait loop blocks the main thread. It freezes UI, delays rendering, and can make your page look broken. Avoid it except for demos.
Busy-wait example (not recommended)
Warning: Do not use in production.
const wait = (ms) => {
const start = Date.now();
let now = start;
while (now - start < ms) {
now = Date.now();
}
};
console.log("Executed now");
wait(1000);
console.log("Executed after 1 second");
Code language: JavaScript (javascript)
The loop keeps the CPU busy until time passes. No clicks, no paints, no smooth scrolling, nothing else gets a turn.
Why the DOM does not update during a busy wait
Browsers update the DOM between tasks, after the call stack clears. A busy-wait never lets the stack clear, so rendering and events get stuck until your loop ends.
And to prove it, see the following example, where the console.log gets fired after 1 second (as we would expect), but both of the DOM writes happen at the same time:
Real-world use cases for a 1-second delay
A 1-second wait is common for UI timing, polling, and simple rate limiting. Use a non-blocking delay so your app stays responsive.
UI delay in the browser (spinner, tooltip)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function showSpinnerForOneSecond() {
const spinner = document.querySelector("#spinner");
spinner.hidden = false;
await delay(1000);
spinner.hidden = true;
}
Code language: JavaScript (javascript)
Polling every second
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function pollStatus(getStatus) {
while (true) {
const status = await getStatus();
if (status === "done") return status;
await delay(1000);
}
}
Code language: JavaScript (javascript)
Rate limiting API calls (1 request per second)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function fetchWithOneSecondGap(urls) {
const results = [];
for (const url of urls) {
const res = await fetch(url);
results.push(await res.json());
await delay(1000);
}
return results;
}
Code language: JavaScript (javascript)
Pitfalls and accuracy, why “1 second” is not always exact
Even with setTimeout(..., 1000), the callback runs “not before 1000ms”, not “exactly at 1000ms”.
Inactive tabs can throttle timers
Browsers often throttle timers in background tabs to save battery and CPU. Your “wait 1 second” might become several seconds if the tab is inactive.
If you need precise scheduling, don’t rely on timers alone. Measure real elapsed time and correct for it.
Minimum delay and timer drift
Heavy CPU work, lots of queued tasks, or a busy event loop can delay timer callbacks. Expect drift under load. If accuracy matters, check Date.now() or performance.now() and adjust.
FAQ
How do I wait 1 second in JavaScript?
setTimeout(() => {
console.log("after 1 second");
}, 1000);
Code language: JavaScript (javascript)
How do I sleep 1 second in JavaScript using async/await?
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
await delay(1000);
Code language: JavaScript (javascript)
Does JavaScript have a built-in sleep() function?
No built-in sleep() for browsers. You typically use setTimeout or await a Promise-based delay(). Blocking “sleep loops” exist, but they freeze the UI and are a bad idea.
How do I wait 1 second in Node.js?
import { setTimeout } from "node:timers/promises";
await setTimeout(1000);
Code language: JavaScript (javascript)
Why does setTimeout not pause the code?
setTimeout schedules your callback for later and returns immediately. JavaScript keeps running the next lines while the timer counts down.
Why is my setTimeout slower in a background tab?
Browsers throttle background tabs. Timers get clamped so they fire less often to save resources, which makes 1000ms delays less reliable.
How do I test setTimeout without waiting in Jest?
Use fake timers and advance time instantly.
jest.useFakeTimers();
test("waits 1 second", () => {
const fn = jest.fn();
setTimeout(fn, 1000);
jest.advanceTimersByTime(1000);
expect(fn).toHaveBeenCalled();
});
Code language: JavaScript (javascript)
Testing, how to handle waits in Jest
Use fake timers instead of real 1-second waits
Real 1-second waits make tests slow and flaky. Fake timers make timing deterministic.
jest.useFakeTimers();
test("waits 1 second", () => {
const fn = jest.fn();
setTimeout(fn, 1000);
jest.advanceTimersByTime(1000);
expect(fn).toHaveBeenCalledTimes(1);
});
Code language: JavaScript (javascript)
Testing async/await delay()
Advance timers, then let pending microtasks run before asserting.
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
jest.useFakeTimers();
test("await delay(1000) resolves", async () => {
const done = jest.fn();
const p = (async () => {
await delay(1000);
done();
})();
jest.advanceTimersByTime(1000);
await Promise.resolve();
await p;
expect(done).toHaveBeenCalled();
});
Code language: JavaScript (javascript)
Conclusion, pick the right approach
For “javascript wait 1 second”, use a non-blocking delay.
- Need async/await? Use
await delay(1000) - Need callback-based code? Use
setTimeout(() => { ... }, 1000) - In Node 15+? Use
node:timers/promisesandawait setTimeout(1000) - Avoid busy-wait loops, they freeze the page
CTA: Copy the delay() helper from above and use await delay(1000) as your default pattern.
![How to Create a Sticky Navbar [CSS & JS] sticky navbar share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/sticky-navbar-share-300x150.png)
![How to Create CSS Animations on Scroll [With Examples] css animation scroll share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/css-animation-scroll-share-300x150.png)
![JavaScript Image Slider [ How To Build One ] javascript slider how to build one big](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/javascript-slider-how-to-build-one-big-300x150.png)


