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.

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Using Named Function with setTimeout()

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

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Passing Arguments to the Function

setTimeout() can pass arguments to the callback function.

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Cancelling setTimeout() using clearTimeout()

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

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3