JavaScript setTimeout()

• Updated August 9, 2022

JavaScript SetTimeout is one of the crucial methods you might come by. If you want to acquire a solid grip in SetTimeout() function in javascript, then this article is for you, let’s get started.

javascript setTimeout

What is SetTimeout() function?

 SetTimeout() method makes a code asynchronous. The code inside the setTimeout runs after the user-defined timer gets completed. The timer is also known as the delay before the code runs inside the setTimeout.


setTimeout(() => { // this is the function (arrow function)
}, timeout); // this is the delay

Here in the setTimeout, we have written a function that will execute after the delay expires. And the delay will be counted in milliseconds.

Example 1:

setTimeout(() => {
    console.log("welcome to IndGeek 👋")
}, 2000);

//welcome to IndGeek 👋

Here we are executing the function after the timer expires. That’s why the arrow function will execute after 2 seconds and print “welcome to IndGeek 👋”.

Example 2

setTimeout((a,b) => {
    console.log("the addition is : ", a + b )
}, 2000,4,6);

// the addition is : 10

We can have arguments inside the function, and we also can pass the value of the argument after the delay. Therefore, it will return the output “the addition is: 10”.

Example 3

 const id = setTimeout((a,b) => {
    console.log("the addition is : ", a + b )
}, 2000,4,6);


Also Read: Javascript Destructuring explained

Timeout {
  _idleTimeout: 2000,
  _idlePrev: [TimersList],
  _idleNext: [TimersList],
  _idleStart: 23,
  _onTimeout: [Function (anonymous)],
  _timerArgs: [Array],
  _repeat: null,
  _destroyed: false,
  [Symbol(refed)]: true,
  [Symbol(kHasPrimitive)]: false,
  [Symbol(asyncId)]: 2,
  [Symbol(triggerId)]: 1
the addition is :  10

The setTimeout function has an id which we have returned. And it returns before the code inside the function. We also can use the id to clear the delay.

Summing Up

Thanks for reading, Here we have learned what the setTimeout() function is. I hope you like the post if you do please share it with others. This is the overall information you need to get started, to learn in-depth about the javascript setTimeout function, you may refer to this tutorial.

Sharing Is Caring:
Saheb Dhara

An aspiring Web designer, Geeky tech hunter. Upcoming ECE engineer.

Leave a Comment