Skip to main content

Command Palette

Search for a command to run...

setTimeout() Function

Published
•3 min read•View as Markdown
setTimeout() Function
H

I am a frontend web developer. I use cutting-edge technology to solve problems. I love peer learning and challenges.

setTimeout() is a method that allows you to execute a function after a specified amount of time has elapsed. It takes two arguments: the first argument is the function that you want to execute, and the second argument is the amount of time in milliseconds that you want to wait before executing the function.

Here's an example:

setTimeout(function mySample() {
  console.log('Hello, world!');
}, 1000);

In this example, we're passing the mySample() to setTimeout(). This function simply logs the string "Hello, world!" to the console. We're also passing the number 1000 as the second argument to setTimeout(), which means that we want to wait for 1000 milliseconds (or 1 second) before executing the function. When you run this code, you'll see that it waits for 1 second before logging "Hello, world!" to the console.

Just like the .addEventListener() code run at some later point, there is the setTimeout() function that will run code at a point later in time. Since setTimeout() is an API provided by the browser, the call to setTimeout() gives the mySample() function over to the browser which starts a timer. After the timer is finished, the mySamplei() function moves to the Queue. If the Call Stack is empty, then the mySample() function is moved to the Call Stack and executed.

When is the call stack said to be empty?
The call stack is said to be empty when there are no more functions waiting to be executed. In other words, the call stack is empty when the program has finished executing all of its functions and has returned to the global execution context. For example, if you have a function cat() that calls another function dog(), the call stack will contain cat() and dog() while dog() is executing. Once dog() has finished executing and returned its value to cat(), dog() is popped off the call stack and the call stack only contains cat() again. Once cat() has finished executing and returned its value to the global execution context, cat() is popped off the call stack and the call stack is empty again.

What happens when setTimeout() is set to 0 milliseconds?

setTimeout(function greeting() {
    console.log('Good day');
}, 0);

“You might think that since it has a delay of 0 milliseconds, the greeting() function would run immediately. However, it still goes through the JavaScript Event Loop. So, the function is handed off to the browser where the browser starts a timer for 0 milliseconds. Since the timer ends immediately, the greeting() function will move to the Queue, and then to the Call Stack once the Call Stack has finished executing any currently-running tasks.

In Summary:

The browser-provided setTimeout() function takes another function and a delay and invokes the function after the delay has passed. Knowing how the JavaScript Event Loop works, we can use the setTimeout() method to help us write code that allows the browser to handle user interactions.