DEV Community

Cover image for What is Scheduling in JavaScript?
Rahul
Rahul

Posted on • Edited on

3

What is Scheduling in JavaScript?

Generally, these two things disturb and irritate a lot when learning JS. In this post, we'll learn how to use them as a beginner.

Sometimes we may decide to execute a function after a certain time delay. That's called "scheduling a call".

  • setTimeout(): This method allows to execute a function only once after the specified time delay.
  • setInterval(): This method is used to execute a function repeatedly, starting after the time interval and then continues with equal intervals.

setTimeout()

This executes the function passed to it only once after the delay time has elapsed. Eg:

function greet() {
    console.log('Hello there!'); 
}
setTimeout(greet,1000); 
// var timerId=setTimeout(...); 
// clearTimeout(timerId); 
Enter fullscreen mode Exit fullscreen mode

Here, we execute the function "greet" with setTimeout(). It will console the output after a delay of 100ms. The setTimeout function returns a timerId which can be used to clear the timeout using the clearTimeout() method using the syntax mentioned in comments.


setInterval()

This executed the function passed to it after the time delay and then keeps on continuing with an equal interval of time delay. Eg:

function greet() {
    console.log('Hello there!'); 
}
setTimeout(greet,1000); 
// var timerId=setInterval(...); 
// clearInterval(timerId); 
Enter fullscreen mode Exit fullscreen mode

Here, we execute the function "greet" with setInterval. It will console after a delay of 1000ms and will continue to do the same after every 1000ms. This also returns a timeId similar to setTimeout() and can be cleared using setInterval() in the same manner.


😎Thank You For Reading | Happy Coding⚡

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (3)

Collapse
 
peerreynders profile image
peerreynders • Edited

these two things disturb and irritate a lot when learning JS.

Really? I'd flip it around and claim that until you understand JavaScript's event loop, task and microtask queues you're going to have a hard time understanding its bread-and-butter asynchronous behaviour given that it has to accomplish work concurrently on a single thread.

setTimeout and setInterval run on the Task queue, Promises (and therefore async/await) run on the microtask queue.

Resources:

Collapse
 
rahxuls profile image
Rahul

Helpful man. Thanks.

Collapse
 
stormkid2009 profile image
Anwar Ahmed

The second code method should be corrected to setInterval. Thanks

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay