JavaScript

What is the purpose of setTimeout() function in JavaScript

When writing a program, it is not always necessary that the program runs immediately or keeps running infinitely. There are many occasions where a delay before executing a portion of the code can prove to be valuable. Similarly, ending a chunk of code at just the right time can also be ideal in many scenarios. To fulfill these needs, JavaScript has a special feature named “setTimeout()”.

In this article, all the details behind the concept and working of the function “setTimeout()” are explained.

What is the objective behind setTimeout()?

Everyone in their lives has made use of a gadget called a timer. Timers or an alarm are used for many various procedures. A timer provides the functionality of knowing when to stop or start certain actions in your daily life. Much like that, the “setTimeout()” function will provide this feature to your code. Using this function will provide delays before any user-defined functions can be executed.

Let’s take an example of this. In a video game, the enemy has to respawn exactly 20 seconds after it is killed. The “setTimeout()” function will be implemented into the original code which is responsible for running the videogame. This function is what will cause the enemy to have a 20-second delay before it spawns again. In the same game, this function can be used for something very contrasting. Suppose an item has appeared for the player. But the game developer does not want that item to linger around for infinity. This is where the “setTimeout()” will step in and give a limited time for that item to linger.

How does the setTimeout() function work?

This syntax is very basic and easy to understand. It takes two parameters within its brackets. The first parameter is the user-defined function that is to be executed. We will take another parameter which is the time of the delay(milliseconds) that will occur before the user-defined function is executed. Below is a sample code for this execution:

function sampleFunction() {
    alert("Timer has run out!");
}
setTimeout(sampleFunction, 5000);

In this example, the function “sampleFunction” will be executed 5 seconds(5000 milliseconds) after the code is run. Refer to the snippet below:

There are a few other details to note about this code as well.

  • The default delay time is 0.
  • The function returns will return the id of the timer.
  • Additional functions can also be passed in versions after “IE9

Conclusion

A “setTimeout()” function is used to provide a delay before the execution of any user-defined function. The function can prove useful when executing or terminating certain chunks of code within a program. In this article, a detailed explanation has been provided on what the “setTimeout()” function is and how it can be used in JavaScript.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.