Nearby lessons

14 of 22

JavaScript: setTimeout() Method

📌 What is setTimeout()?

The setTimeout() method is used to execute a function after a specified delay (in milliseconds). It is a part of JavaScript's asynchronous behavior and does not block code execution.

1️⃣ Basic Usage of setTimeout()

The function will run once after a delay of specified milliseconds.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ Using Named Function with setTimeout()

You can pass a function name to setTimeout() instead of an anonymous function.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Passing Arguments to the Function

setTimeout() can pass arguments to the callback function.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ Cancelling setTimeout() using clearTimeout()

You can cancel the delayed function using clearTimeout() by storing the timeout ID.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - setInterval