title: Run Javascript function in background of NodeJS server
body: <p>I have written a function that periodically checks the battery statuses of connected android devices and returns an array. How can I run this function on server startup and have it continuously running while providing its information to other pages?</p>
  
  <pre><code>var minutes = 1, the_interval = minutes * 60 * 500;
  setInterval(function() {
  adb.devices().then(function(devices) {
  
      var promises = new Array();
  
      for (var i = 0; i &lt; devices.length; i++){
          promises.push(adb.checkBattery(devices[i]));
      }
  
      Promise.all(promises).then(function(availableDevices) {
          console.log('Updated:');
          console.log(availableDevices);
          return availableDevices;
      });
  });
  }, the_interval);
  </code></pre>
  
  <p>This is my app.js file which was automatically created when I opened a new project. I added app.use for the various routes I have created.</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var multer = require('multer');
  var adb     = require('./adb.js');
  
  var index = require('./routes/index');
  var users = require('./routes/users');
  var devices = require('./routes/devices');
  var openBrowser = require('./routes/openBrowser');
  var closeBrowser = require('./routes/closeBrowser');
  var openApp = require('./routes/openApp');
  var closeApp = require('./routes/closeApp');
  var install = require('./routes/install');
  var uninstall = require('./routes/uninstall');
  var pull = require('./routes/pull');
  var push = require('./routes/push');
  var battery = require('./routes/battery');
  
  
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', index);
  app.use('/users', users);
  app.use('/devices', devices);
  app.use('/openBrowser', openBrowser);
  app.use('/openApp', openApp);
  app.use('/closeApp', closeApp);
  app.use('/install', install);
  app.use('/closeBrowser', closeBrowser);
  app.use('/uninstall', uninstall);
  app.use('/pull', pull);
  app.use('/push', push);
  app.use('/battery', battery);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  </code></pre>