DEV Community

loading...
Cover image for Javascript Timer APIs

Javascript Timer APIs

Anuradha Aggarwal
Software Engineer
Originally published at anuradha.hashnode.dev ・1 min read

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)
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

setTimeout2.gif

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);
Enter fullscreen mode Exit fullscreen mode

setInterval.gif

Wrap Up!!

Thank you for your time!! Let's connect to learn and grow together.

LinkedIn Twitter
Instagram

Buy-me-a-coffee

Discussion (0)

Forem Open with the Forem app