JavaScript recurring timers with setInterval

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we have a good understanding of how JavaScript setTimeout works to delay a function.
Let's look at how we can perform an action every x time.
This can be super helpful for animating stuff or checking a data feed.
Let's look at how this will work in its most basic form.
setInterval(() => {
// Run every 100 milliseconds
}, 100);
This function will run every 100 milliseconds.
Often you might want to achieve this to run only until a specific condition is met.
We can clear the interval by using the clearInterval.
const timer = setInterval(() => {
// Do something
}, 100);
clearInterval(timer);
Or you can even stop it from inside the interval.
const timer = setInterval(() => {
if (condition) {
clearInterval(timer);
return;
}
// Execute the function
}, 100);
This is a great way to stop a particular action from running.
When you use setInterval, it does not care how long your function runs.
Meaning it will always start a new loop at the set time.
For example, when you use it to animate, but the animations have different lengths, it might cause weird side-effects where the following animation will start, and the first one only just finished.

As you can see, each function can have its own time to execute.
If you find yourself needing them to wait a specific time, using setTimeout might be a better solution.
We can set up a recursive setTimeout function. That is a function that calls itself once it's done doing its thing.
const coolFunc = () => {
// Execute the function
setTimeout(coolFunc, 100);
};
setTimeout(coolFunc, 100);
Which will result in the following flow.

If you are keen to see some more real-world examples, here is a list of articles using them.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter