DEV Community

Ranvir Singh
Ranvir Singh

Posted on • Updated on • Originally published at ranvir.xyz

Run a function periodically in vanilla Javascript

This post was originally posted on my blog.

In a production system, we run a lot of scripts periodically either to sync data from some third-party source or spit data to some source. Although there are a lot of solutions to achieve such periodic behavior, in this post we are going to discuss one such solution using vanilla JavaScript.

Use cases

  • Taking backups of the database.

Taking a backup of the database can be done once or twice a day depending upon the use case.

If you get a lot of users you can take this backup multiple times a day.

AWS takes automatic backups so you don't have to worry about it.

  • Sending newsletters to your customers.

Sending newsletters to customers can also be one of the use cases of running things periodically.

Possible Solutions

Crontab

Most of the organizations tend to use crontab for handling any such requests for running periodic tasks.

Each and every language has it's own implementation of handling periodic tasks.

Periodic task in Vanilla JavaScript

Let's write a simple function that can log the current time after every minute.

const tellTime = async function () {
    console.log(new Date());
}

const minutes = 1;
const interval = minutes * 60 * 1000;

setInterval(function() {
    // catch all the errors.
    tellTime().catch(console.log);
}, interval);
Enter fullscreen mode Exit fullscreen mode

First of all, we declare an async function tellTime which logs the time whenever it is called.

It is important to declare this function async so that we can run .then or .catch after this function.

setInterval is the inbuilt function that can run any statements after every given interval.

It takes two arguments, first is the definition of the function being run itself and next is the interval after which we want to run the function again( In milliseconds).

In this example, we have set the interval to one minute.

Here is how the output is going to look like

2019-11-18T18:40:27.286Z
2019-11-18T18:40:57.293Z
...
Enter fullscreen mode Exit fullscreen mode

Although this is a pretty straight forward example you can extend this to do anything that you want.

Top comments (5)

Collapse
 
alexandrusimandi profile image
Alexandru Simandi

We should take note that because it's single threaded there is a risk that the app can go in an undefined state and block the interval from the event loop. Imagine a while true after setting the interval

Collapse
 
singh1114 profile image
Ranvir Singh

Yes, the risk is there. That's why people prefer to use combination of cron and serverless functions for this.

Collapse
 
asolace profile image
Jack H.

Will this cause a memory leak if it's run indefinitely?

Collapse
 
singh1114 profile image
Ranvir Singh

@asolace I don't think so, all the variables are initialized inside the tellTime function. So, they should be used only when the program runs after each instance. Once the function ends, the garbage collector should remove them from the heap.

stackoverflow.com/questions/140341...

Collapse
 
singh1114 profile image
Ranvir Singh

@asolace @alexandrusimandi updated the post ranvir.xyz/blog/running-a-function... with the answer to the memory leak. Hope it helps.