utils.js
utils.js
This is for:
DeveloperIn this article
The utils.js file provides a convenient space for defining common functions and variables that can be reused across experience files.
Example usage
In this example, a length of time is computed and reused in both the triggers.js
file and the variation.js
files:
module.exports = function utils (options) {
const howLong = require('how-long')
const xmas = new Date()
xmas.setMonth(11, 25)
return howLong(new Date(), xmas, 'weeks')
}
module.exports = function triggers (options, cb) {
const getTimeLeft = require('./utils')
const weeks = getTimeLeft().weeks
if (weeks < 4) cb()
}
module.exports = function variation (options) {
const getTimeLeft = require('./utils')
const weeks = getTimeLeft().weeks
document.getElementById('countdown').textContent = `Only ${weeks} weeks left!`
}