title: Poll an Express server with $.get regularly
body: <p>There is an Express server with a server time generator. The generated time are sent to the client page <code>gtm.hbs</code>. How do I poll the server regularly from the client page? I’m looking for an ajax solution.</p>
  
  <p>Here’s the server side code stored in <code>\routes\getTime.js</code>:</p>
  
  <pre><code>module.exports = {  
      sendCurrentUTCTimeString: function(req, res, next) {
          var currentUTCTime = new Date();
          var currentUTCHours = currentUTCTime.getUTCHours();
          var currentUTCMinutes = currentUTCTime.getUTCMinutes();
          var currentUTCSeconds = currentUTCTime.getUTCSeconds();
  
          currentUTCHours = ( currentUTCHours &lt; 10 ? "0" : "" ) + currentUTCHours;
          currentUTCMinutes = ( currentUTCMinutes &lt; 10 ? "0" : "" ) + currentUTCMinutes;
          currentUTCSeconds = ( currentUTCSeconds &lt; 10 ? "0" : "" ) + currentUTCSeconds;
          currentUTCHours = ( currentUTCHours == 24 ) ? "00" : currentUTCHours;
  
          function pad(number, length) {
              var str = "" + number;
              while (str.length &lt; length) {
                  str = '0' + str;
              }
              return str;
          }
  
          var offset = new Date().getTimezoneOffset();
          offset = ((offset &lt; 0 ? '+' : '-') + pad(parseInt(Math.abs(offset / 60)), 2) + pad(Math.abs(offset % 60), 2));
          offset = offset.match(/[\+-][0-9][0-9]/)[0];
          res.send({currentUTCTime: currentUTCTime, currentUTCHours: currentUTCHours, currentUTCMinutes: currentUTCMinutes, currentUTCSeconds:currentUTCSeconds, offset: offset});
      }
  }
  </code></pre>
  
  <p>The <code>gettime.hbs</code> page code is</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          setInterval(function() {
              var currentUTCHours;
              var currentUTCMinutes;
              var currentUTCSeconds;
              var offset;
              $.get('/gettime', function(data) {
                  debugger;
                  console.log(data);
                  currentUTCHours = data.currentUTCHours;
                  currentUTCMinutes = data.currentUTCSeconds;
                  currentUTCSeconds = data.currentUTCMinutes;
                  offset = data.offset;
                  $('#currentUTCHours').text(currentUTCHours);
                  $('#currentUTCMinutes').text(currentUTCMinutes);
                  $('#currentUTCSeconds').text(currentUTCSeconds);
                  $('#offset').text(offset);
              });
  
              console.log('currentUTCHours: ' + currentUTCHours);
          }, 1500);
      });
  &lt;/script&gt;
  
  currentUTCHours: &lt;span id="currentUTCHours"&gt;&lt;/span&gt;&lt;br&gt;
  currentUTCMinutes: &lt;span id="currentUTCMinutes"&gt;&lt;/span&gt;&lt;br&gt;
  currentUTCSeconds: &lt;span id="currentUTCSeconds"&gt;&lt;/span&gt;&lt;br&gt;
  offset: &lt;span id="offset"&gt;&lt;/span&gt;&lt;br&gt;
  </code></pre>
  
  <p>Here I wanted to render a page that would poll the server then fill the four <code>&lt;span&gt;</code>s with the server response values.</p>
  
  <p>The route to connect the server and the client is</p>
  
  <pre><code>app.get('/gettime', getTime.sendCurrentUTCTimeString);
  app.get('/gtm', function(req, res, next) {
      res.render('gettime', {});
  }); 
  </code></pre>