Hello Developers!! Today we will discuss the different timers API used in Javascript
- setTimeout
- setInterval
setTimeout
It delays an operation for a certain amount of time which is passed as a parameter.
It takes two parameters:
- a function
- delay time in millisecond
setTimeout(()=>{
console.log("setTimeout in action");
}, 2000)
In the above code snippet, it will print to the console after 2000 ms.
Now look at another example of setTimeout
:-
let myFunc = (num1, num2) => {
console.log(num1, "---", num2);
}
setTimeout(myFunc, 2000, 2, 3);
If you pass more than 2 parameters in the setTimeout
then this will eventually become the parameters of the myFunc
.
setInterval
It puts a delayed operation in a loop
setInterval(()=>{
console.count("setInterval in action")
},2000);
Wrap Up!!
Thank you for your time!! Let's connect to learn and grow together.
Top comments (0)