Introduction
This is about the third argument in setTimeout function.
As we know, setTimeout allows us to run a function once after the interval of time.
This is the general syntax,
let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)
In day to day usage, we use setTimeout() like this:
function greeting(){
alert('hey!!')
}
setTimeout(greeting,1000);
Let's see how we can pass the third argument
function greeting(arg1, arg2){
console.log(arg1,arg2)
}
setTimeout(greeting,1000,"Hi", "There");
//output: Hi There
Instead of string we can pass an array, object, etc.
function greeting(arr){
console.log(arr);
}
const arr = [1,2,3,4]
setTimeout(greeting,1000,arr);
//output: (4) [1, 2, 3, 4]
function greeting(person){
console.log(person);
}
const person = {name:"abc", age: 21}
setTimeout(greeting,1000,person);
//output: {name: "abc", age: 21}
That's how you can make use of the third argument.
Cheers !!!
Top comments (1)
great