Featured Article

W3Schools JavaScript Delay: Master setTimeouts and setTimeout Alternatives for Perfect Timing Control

Adam Jul 13, 2026

Ever found yourself in need of a delay function in JavaScript? A use case could be for an animation, a timer, or simply to ensure code execution happens sequentially. W3Schools, an online web development learning resource, provides a handy JavaScript delay method. Let's delve into how you can use it effectively.

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

JavaScript, being single-threaded, doesn't inherently support delays or pausing execution. However, you can introduce delays using several methods. W3Schools primarily discusses two approaches: using setTimeout method and async/await.

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

Delay using setTimeout Method

The setTimeout() method is a built-in JavaScript function that delays the execution of an anonymous function. It takes two parameters: the function to execute and the delay time in milliseconds.

15 JavaScript Array Methods You Should Master Today
15 JavaScript Array Methods You Should Master Today

Here's an example from W3Schools where an alert message is displayed after a 5-second delay:

setTimeout(function() { alert("Hello"); }, 5000);

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Delayed Function Execution

Delayed functions can also be used to ensure code runs in sequence. For instance, if you have two dependent animations, delaying the second ensures it starts only after the first completes.

Let's take a simple example of two console log statements where the second is delayed by 2 seconds:

"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"
"Master JavaScript's Slice Method �: Easy Array & String Slicing Tips! 🚀 #CodingTips #WebDev"

console.log("Starting...");
setTimeout(function() { console.log("Finished!"); }, 2000);

Passing Arguments to Delayed Function

You might want to include arguments in your delayed function. To achieve this, you need to wrap the function inside its own scope. Here's how you can do it:

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

var a = "Hello";
setTimeout(function() { console.log(a); }, 1000);

Delay using async/await

Javascript Fundamental
Javascript Fundamental
setTimeout in JavaScript
setTimeout in JavaScript
an info sheet with different types of web pages
an info sheet with different types of web pages
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
Javascript detect dark mode | javascript tutorial
Javascript detect dark mode | javascript tutorial
Javascript String Methods
Javascript String Methods
7 JavaScript Tricks That Save Hours of Coding | Developer Tips & Shortcuts
7 JavaScript Tricks That Save Hours of Coding | Developer Tips & Shortcuts
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Recursion Simplified
JavaScript Recursion Simplified
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
JavaScript study guide for fast learning
JavaScript study guide for fast learning
Javascript setTimeout() method
Javascript setTimeout() method
JavaScript calculator projet
JavaScript calculator projet
Functions in Javascript
Functions in Javascript
Learn JavaScript in 30 Days
Learn JavaScript in 30 Days
How to Declare Variables in JavaScript
How to Declare Variables in JavaScript
Simple Stopwatch With Pure Javascript
Simple Stopwatch With Pure Javascript

Async/await is a newer syntax for managing asynchronous operations in JavaScript. It's more readable and easier to work with than callbacks or promises. Async/await uses the Promise object under the hood.

To introduce a delay, you can use the Promise object's resolve function or the built-in function delay from W3Schools:

let delay = ms => new Promise(res => setTimeout(res, ms));

Using async/await for Delays

Here's an example where async/await delays the console log by 5 seconds:

async function greet() {
  console.log("Hello");
  await delay(5000);
  console.log("World!");
}

Cascading Delays with async/await

You can chain delays using async/await to ensure code executes in a specific order. Here's an example with three console log statements delayed by 1 second, 2 seconds, and 3 seconds respectively:

async function greet() {
  console.log("Hello");
  await delay(1000);
  console.log("World!");
  await delay(2000);
  console.log("Again!");
  await delay(3000);
  console.log("Finally!");
}

Mastering these delay techniques can greatly enhance your JavaScript skills, enabling you to create complex animations, manage asynchronous code, and more. Now, go ahead and add these methods to your JavaScript toolkit. Happy coding!