title: Cron Job every 10 seconds with Node-Cron
body: <p>I am using to Node-Cron run a cron job on every 10 seconds. but it seems like there is a issue with node-cron. <a href="https://github.com/ncb000gt/node-cron#cron-ranges" rel="noreferrer">https://github.com/ncb000gt/node-cron#cron-ranges</a></p>
  
  <p>i made a request at <code>04:28:34</code> but the script started at <code>4:28:40</code>(should start at <code>04:28:44</code>)</p>
  
  <p>i made a request at <code>04:48:58</code> but the script started at <code>04:49:00</code> (should start at <code>04:49:08</code>)</p>
  
  <p>i made a request at <code>05:03:45</code> but the script started at <code>05:03:50</code> (should start at <code>04:49:08</code>)</p>
  
  <p>below is my code to execute this task.</p>
  
  <pre><code>router.post('/secret', function (req, res) {
      console.log('Post request!');
      console.log('Start At:' + getUTCDateTime());
  
      function getUTCDateTime() {
  
          var date = new Date();
          var dateUTC = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
          var hour = dateUTC.getHours();
          hour = (hour &lt; 10 ? "0" : "") + hour;
          var min = dateUTC.getMinutes();
          min = (min &lt; 10 ? "0" : "") + min;
          var sec = dateUTC.getSeconds();
          sec = (sec &lt; 10 ? "0" : "") + sec;
          var year = dateUTC.getFullYear();
          var month = dateUTC.getMonth() + 1;
          month = (month &lt; 10 ? "0" : "") + month;
          var day = dateUTC.getDate();
          day = (day &lt; 10 ? "0" : "") + day;
          return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec; // format: 2015-04-10 11:40:50
  
      }
  
      function getEndDateTime(startAt, callback) {
  
          var date = new Date(startAt);
          date.setSeconds(date.getSeconds() + parseInt(callback));
          var sec = date.getSeconds();
          sec = (sec &lt; 10 ? "0" : "") + sec;
          var hour = date.getHours();
          hour = (hour &lt; 10 ? "0" : "") + hour;
          var min = date.getMinutes();
          min = (min &lt; 10 ? "0" : "") + min;
          var year = date.getFullYear();
          var month = date.getMonth() + 1;
          month = (month &lt; 10 ? "0" : "") + month;
          var day = date.getDate();
          day = (day &lt; 10 ? "0" : "") + day;
          return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec; // format: 2015-04-10 11:40:50
  
      }
  
      function getStartDateTime(startAt, callback) {
  
          var date = new Date(startAt);
          date.setSeconds(date.getSeconds() - parseInt(callback));
          var sec = date.getSeconds();
          sec = (sec &lt; 10 ? "0" : "") + sec;
          var hour = date.getHours();
          hour = (hour &lt; 10 ? "0" : "") + hour;
          var min = date.getMinutes();
          min = (min &lt; 10 ? "0" : "") + min;
          var year = date.getFullYear();
          var month = date.getMonth() + 1;
          month = (month &lt; 10 ? "0" : "") + month;
          var day = date.getDate();
          day = (day &lt; 10 ? "0" : "") + day;
          return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec; // format: 2015-04-10 11:40:50
  
      }
  
      function callBackUrl(url, data){
          var request = require('request');
  
          request.post(
              url,
              {form: {data: data}},
              function (error, response, body) {
                  if (!error &amp;&amp; response.statusCode == 200) {
                      console.log(body)
                  }
              }
          );
  
      }
  
      var startAt = req.body.startAt;
      var callback = req.body.callback;
      var endAt = req.body.endAt; //2015-05-15 11:23:50
      var data = req.body.data;
      if (callback == "" || endAt == "" || data == "")
          res.end('Params cannot be empty: ' + JSON.stringify(req.body));
  
      if (getUTCDateTime() &gt;= endAt)
          res.end('Param "endAt" is invalid: ' + JSON.stringify(req.body));
  
      var jsonObject = JSON.parse(data); //to get the JSON object of the string
  
  
      var callbackUrl1 = 'http://qa.www.com/index.php/api/default/test/id/1';
      var callbackUrl2 = 'http://qa.www.com/index.php/api/default/test/id/2';
      //cronStartAt = '0 */' + callback + ' * * * *'; // only every 1 minute
      cronStartAt = '*/' + callback + ' * * * * *'; // only every 1 minute
      var nowCount = 0;
      var CronJob = require('cron').CronJob;
      var cron = new CronJob(cronStartAt, function () {
          nowCount++;
          var tmpStartAt = getStartDateTime(getUTCDateTime(), callback);
          var tmpEndAt = getUTCDateTime();
          //var tmpStartAt = getUTCDateTime();
          //var tmpEndAt = getEndDateTime(tmpStartAt, callback);
          //console.log(tmpStartAt + ' ' + tmpEndAt);
          jsonObject.startAt = tmpStartAt;
          jsonObject.endAt = tmpEndAt;
          data = JSON.stringify(jsonObject);
  
          callBackUrl(callbackUrl1, data);
          if (getUTCDateTime() &gt;= endAt) {
              cron.stop();
              console.log('stopped at: ' + getUTCDateTime());
              console.log('now count: ' + nowCount);
          }
      }, function () {
          /* This function is executed when the job stops */
          // on crone stop another http call with submitted post data
          callBackUrl(callbackUrl2, data);
      }, true, 'America/Los_Angeles');
  
      cron.start();
      console.log("Post body: %j", req.body);
      res.end('Body: ' + JSON.stringify(req.body));
  
  });
  </code></pre>