Skip to main content

Command Palette

Search for a command to run...

Javascript Timer APIs

Updated
1 min read
Javascript Timer APIs
A

A software engineer who likes to explore new technologies, problem-solving, build projects, and have a keen interest in Web development.

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);

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);

setInterval.gif

Wrap Up!!

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

LinkedIn Twitter Instagram

Buy-me-a-coffee

More from this blog

<

<DeveloperBlogs />

43 posts

Hello folks!! 👋 I'm Anuradha Aggarwal, a software engineer who likes to explore new technologies, problem-solving, build projects 👩‍💻, and have a keen interest in web development & writing blogs ✍.