How to Automate or Schedule Actions in JavaScript

During the initial introduction, JavaScript was nothing mere than a tool facilitating the validation of HTML form elements. It was primarily used for scripting web pages, later got implied to animate and dynamize websites in large by the public.

To say, JavaScript’s revolution started from this point. Soon, JavaScript evolved into the statue of an all-purpose language. Currently, the extensiveness of Javascript application is large that, it is implied on large variety systems running from mobile applications to web applications. And it has a countless number of libraries and frameworks. node.js, Angular.js vue.js are some popular examples.

In the current scenario, JavaScript is everywhere and JavaScript is used to create both Android and IOS applications.

In this blog, we discuss automating actions in JavaScript.

Automation Using setInterval Method

The setInterval method is used to set repeated actions. Once we set a time interval for an action it will be repeated forever until we remove the interval from the action.

Setting an Interval:

Output:

Removing Interval From An Action

Let’s see how to remove a given interval from an action. For removing interval callback of action, one needs to use the clearInterval method. Let’s see how to execute the function.

Setting an Interval:

//just defines a function

var count = 0;

//defines the interval variable here that the function can use it

var intervalVariable;

function foo()

{

console.log(‘Hello World ’+count);

Count++;

if(count===3)

{

clearInterval(intervalVariable);

}

}

//here comes the master

intervalVariable=setInterval(foo, 1000);

//the time is given in milliseconds

Scheduling An Action Using setTimeout

The setTimeout method in JavaScript enables you to set the action to be done in future. That is an action that to be performed after a given time. For this, we need to give a timeout in milliseconds and it will get eventually executed after that time.

//just defines a function

function foo()

{

console.log(‘Hello World ’);

}

//Here this will work after 60 seconds

setTimeout(foo(), 60 * 1000)

Schedule Actions Using node-schedule

This package helps the end user to schedule actions like a cron. We can specify the time for a job or specify date and time when the job is to be done. One can even set up repeated actions under this. To enable the aforesaid, the end user has to install the node-schedule package.

You can install it using npm as follows:

$npminstall node-schedule

Now we can use it for,

Cron Style Scheduling:

* * * * * *

- - - - - -

¦ ¦ ¦ ¦ ¦ ¦

¦ ¦ ¦ ¦ ¦ + day of week (0-7) (0 or 7 is Sun)

¦ ¦ ¦ ¦ +----- month (1-12)

¦ ¦ ¦ +---------- day of month (1-31)

¦ ¦ +--------------- hour (0 - 23)

¦ +-------------------- minute (0-59)

+------------------------- second (0-59, OPTIONAL)

Example:

var schedule =require('node-schedule');

var j = schedule.scheduleJob('35 * * * *', function(){

console.log(‘the time is 35 minutes!’);

});

Repeated Actions:

One can schedule repeated action using this package so that it will be repeated in a time interval.

var schedule =require('node-schedule');

var j = schedule.scheduleJob('0 17 ? * 0,4-6', function(){

console.log('Repeated in every 5 Minutes!');

});

Scheduling Jobs For a Specific Date and Time:

While using this package one can schedule actions for a specific time on a date, let us see an example.

Example:

var schedule =require('node-schedule');

varj = schedule.scheduleJob('0 1 * * *', function(fireDate){

console.log('This job was supposed to run at '+ fireDate +', but actually ran at ' + newDate());

});

Hope the article was insightful. If you have any comments or queries, kindly drop your message to info@cybrosys.com.