Modules
In earlier chapters, we discussed PhantomJS's built-in modules. Let us explore how to create our own modules and use them in our PhantomJS scripts.
First, let's create a straightforward module of a timer that can measure the duration of a given process based on calling the start
and stop
functions. Let us define the variables to be used. We will require timeStart
, timeStop
, and a duration
variable to hold the duration when the stop
function is called.
In a normal JavaScript, we will define our variable as the following:
var timeStart = null;
However, for our modules, we need to use the exports
keyword to indicate that the variables belong to the external module, so let us change our previous code to the following:
exports.timeStart = null; exports.timeStop = null; exports.duration = 0;
These variables are now available in our module. We can now use them within our scripts. We can access them and set new values or display the current data. Let us do this before proceeding. We...