Featured Article

W3Schools JavaScript Wait Tutorial: Master The Pause

Adam Jul 13, 2026

If you're learning JavaScript or are new to async programming, you might be wondering how to implement a pause or delay in your code. While there isn't a built-in `wait` command in JavaScript, we can use available features to achieve this functionality, often using the `setTimeout` function, promises, or async/await syntax. In this guide, we'll explore how to introduce delays and pauses in JavaScript execution, helping you to better control your program's flow and improve performance.

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

Understanding the need for delays is crucial in JavaScript, especially when dealing with heavy computations, API calls, or user interactions. Delays can improve user experience, prevent performance issues, and allow for better code organization. Let's dive into the different ways you can introduce waits and pauses in your JavaScript code.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Using `setTimeout` for Delays

The `setTimeout` function is one of the most straightforward ways to create a delay in JavaScript. It takes two arguments: a callback function and the number of milliseconds you want to wait before executing that function.

⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨  Pin Description:
⏳ Master JavaScript setTimeout(): Delay Code Execution Like a Pro! 💻✨ Pin Description:

Here's a simple example:

```javascript console.log("Start"); setTimeout(function() { console.log("Delayed until now."); }, 2000); ```

In this example, "Start" is logged immediately, and "Delayed until now." is logged after a 2-second wait, demonstrating the delay effect.

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Delaying Code Blocks with `setTimeout`

You can also use `setTimeout` to delay an entire code block by wrapping it in an anonymous function:

```javascript console.log("Start"); setTimeout(function() { // Code to be delayed console.log("Long running task completed."); }, 2000); console.log("End"); ```

In this case, "Long running task completed." is delayed, but "End" is executed immediately, showing the delay's impact on the code block.

Learn JavaScript in 30 Days
Learn JavaScript in 30 Days

Chaining Delays with `setTimeout`

To create multiple delays or implement a countdown, you can chain `setTimeout` functions:

```javascript for (let i = 5; i > 0; i--) { setTimeout(function() { console.log(i); }, (5 - i) * 1000); } ```

This script counts down from 5 to 1, with each number being logged after a 1-second wait.

Javascript setTimeout() method
Javascript setTimeout() method

Using Promises for Delays

Promises offer another approach to handling delays in JavaScript. They can be useful when dealing with asynchronous operations, like API calls or user interactions. Here's how you can use promises to create a delay:

an info sheet with different types of web pages
an info sheet with different types of web pages
Javascript String Methods
Javascript String Methods
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript|async, awaitを利用して非同期処理を制御 - わくわくBank
JavaScript|async, awaitを利用して非同期処理を制御 - わくわくBank
JavaScript calculator projet
JavaScript calculator projet
add active class in javascript
add active class in javascript
a flyer for an upcoming event with information about the date and time in each section
a flyer for an upcoming event with information about the date and time in each section
⭐️🎀 JavaScript Visualized: Promises & Async/Await
⭐️🎀 JavaScript Visualized: Promises & Async/Await
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
Back to Basics - JavaScript Closures
Back to Basics - JavaScript Closures
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
Top JavaScript Tricks Every Developer Should Know
Top JavaScript Tricks Every Developer Should Know
Master JavaScript: Essential Programming Resources 👨‍💻💡
Master JavaScript: Essential Programming Resources 👨‍💻💡
Javascript Project Ideas 👍
Javascript Project Ideas 👍
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)

Creating a Delay with `new Promise`

You can create a promise that resolves after a specific delay using the `new Promise` constructor:

```javascript function delay(ms) { return new Promise((resolve) => { setTimeout(() => { resolve(); }, ms); }); } delay(2000).then(() => console.log("Promise resolved after 2-second delay.")); ```

In this example, "Promise resolved after 2-second delay." is logged only after a 2-second wait.

Using `Promise.resolve` for Delays

You can also use `Promise.resolve` to create a promise that resolves after a specified delay:

```javascript Promise.resolve().then(() => { // Delay the execution of this code by including a setTimeout setTimeout(() => { console.log("Promise resolved after 2-second delay."); }, 2000); }); ```

Both promises and `setTimeout` have their use cases, so understanding how to use them both is essential for versatility in your JavaScript development.

Using `async/await` Syntax for Delays

The `async/await` syntax provides a more readable and synchronous-looking solution for handling delays and asynchronous operations in JavaScript. Here's how you can use `async/await` to create a delay:

Creating a Delay with `await`

You can use `await` together with `Promise.resolve` to create a delay:

```javascript async function delayedGreeting() { await new Promise((resolve) => setTimeout(resolve, 2000)); console.log("Hello, world! Delayed by 2 seconds."); } delayedGreeting(); ```

This function logs "Hello, world! Delayed by 2 seconds." only after a 2-second wait.

Chaining async Delays with `await`

You can easily chain delays using `async/await` syntax:

```javascript async function countdown() { for (let i = 5; i > 0; i--) { await new Promise((resolve) => setTimeout(resolve, 1000)); console.log(i); } } countdown(); ```

This script counts down from 5 to 1, with each number being logged after a 1-second wait in a more readable format than using `setTimeout`.

When working with delays in JavaScript, it's essential to choose the right tool for the job. Understanding `setTimeout`, promises, and `async/await` syntax will enable you to create efficient, performant, and well-structured code. Keep practicing and exploring these concepts to become proficient in JavaScript's rich async landscape, and remember, the best way to learn is by doing! Happy coding!